[x]
Posted via EE Mobile

Search, ask, and monitor your questions on the go with EE Mobile. Visit Experts Exchange from your mobile device and never be out of touch again.

Question
[x]
Attachment Details
[x]
The Solution Rating System

With so many solutions, how can you tell which solutions are most likely to help you and which ones are not? To provide you with a tool to use, we rate our solutions based on various elements that most accurately determine if a solution is a quality solution. To explain what factors affect the solution rating, here are the elements we take into consideration when formulating our solution rating.

  • The Grade of the Solution
  • The Zone Rank of the Expert Providing the Solution
  • The Number of Author and Expert Comments
  • The Number of Experts Contributing
  • The Feedback of the Community

Your Input Matters
Because of the way the system is set up, the most important variable in this equation is you. As a member of Experts Exchange, you are able to cast your vote on the quality of the solutions in regard to how complete, accurate, helpful and easy to understand each solution is. When you provide your feedback, each rating is adjusted accordingly. So, if you see a solution that has a poor rating that you think is a good solution, let us know by rating it. As you do, the rating will be adjusted and will become more accurate for other members of our site.

If you have any suggestions that you would like to make for our rating system, please ask a question in the Suggestions Zone of Community Support.

Thank you!

7.6

SDL Keyboard Input Problems (Multiple Key Presses, Hardware).

Asked by maeve100 in Game Programming, Game Programming User Interface, C++ Programming Language

Tags: SDL Keyboard Key Presses Press Multiple Many Hardware

Hello,

I have been using SDL for the first time, attempting to write a simple game in C++ (MVC++ 2008). Everything has been going great except now I've run into a problem with my keyboard input.

At this point the goal of the program is to allow two balls to be moved around the screen independently and simultaneously, with a held key resulting in continued movement. There are plenty of examples online and fundamentally the code works. I can move both balls independently just fine. I can also move the both simultaneously left/right/up/down without trouble.

The Problem: Very specific key combinations result in one of the two balls not being able to move.

IE: Pressing the down arrow key and the right arrow key together (Moving ball #2 diagonally) makes it so ball #1 cannot be moved at all.

Other key combinations either work fine or generate their own non-functioning directions.  

The combinations are not random and have stayed the same through out my troubleshooting.

What I've Tried: I've tried two different methods for reading the keyboard and storing the information (Both present in the code provided, with one commented out). Both seem to run exactly the same, with the same problems arising. I also tried switching from the arrow keys to jikl. This configuration has the same type of problems, however the problematic directional combinations have changed.

Speculation: I currently think that this is due to my keyboard's  physical hardware. IE when I enter notepad and hold the down and right arrows, I am similarly unable to use asdw.  I have read online that this can be a problem.

Question: If this is the problem, how do I get around it (in a keyboard independent manner)? How do commercial games deal with this issue?  If this isn't the problem, did I miss something stupid? I am still a little shaky on sdl events.

Thank you.
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:
47:
48:
49:
50:
51:
52:
53:
54:
55:
56:
57:
58:
59:
60:
61:
62:
63:
64:
65:
66:
67:
68:
69:
70:
71:
72:
73:
74:
75:
76:
77:
78:
79:
80:
81:
82:
83:
84:
85:
86:
87:
88:
89:
90:
91:
92:
93:
94:
95:
96:
97:
98:
99:
100:
101:
102:
103:
104:
105:
106:
107:
108:
109:
110:
111:
112:
113:
114:
115:
116:
117:
118:
119:
120:
121:
122:
123:
124:
125:
SDL_Event keyevent;    //The SDL event that we will poll to get events.
 
//Variables for the first method I tried.
/*	
int running = 1;
int left = 0;
int right = 0;
int up = 0;
int down = 0;
int lefta = 0;
int rightd = 0;
int upw = 0;
int downs = 0;
*/
 
// Variables for the second method I tried.
int running = 1;
Uint8* keystates;
keystates = SDL_GetKeyState(0);  // or SDL_GetKeyState(NULL)
 
while (running)
{
	
        //Second Method
        while(SDL_PollEvent(&keyevent))
	{
	   if(keyevent.type == SDL_KEYDOWN)
	   {
			keystates[keyevent.key.keysym.sym] = true;
	   }
	   else if(keyevent.type == SDL_KEYUP)
	   {
	        keystates[keyevent.key.keysym.sym] = false;
	   }
	}
	
// ...
        //First method.
	/*
	while (SDL_PollEvent(&keyevent))   //Poll our SDL key event for any keystrokes.
	{
		if (keyevent.type == SDL_KEYDOWN)
		{
			switch(keyevent.key.keysym.sym)
			{
				case SDLK_LEFT:
					left = 1;
				  break;
				case SDLK_RIGHT:
					right = 1;
				  break;
				case SDLK_UP:
					up = 1;
				  break;
				case SDLK_DOWN:
					down = 1;
				  break;
				case SDLK_a:
					lefta = 1;
				  break;
				case SDLK_d:
					rightd = 1;
				  break;
				case SDLK_w:
					upw = 1;
				  break;
				case SDLK_s:
					downs = 1;
				  break;
				case SDLK_ESCAPE:
					running = 0;
				  break;
				default:
				  break;
			}
		}
		if (keyevent.type == SDL_KEYUP)
		{
			switch(keyevent.key.keysym.sym)
			{
				case SDLK_LEFT:
					left = 0;
				  break;
				case SDLK_RIGHT:
					right = 0;
				  break;
				case SDLK_UP:
					up = 0;
				  break;
				case SDLK_DOWN:
					down = 0;
				  break;
				 case SDLK_a:
					lefta = 0;
				  break;
				case SDLK_d:
					rightd = 0;
				  break;
				case SDLK_w:
					upw = 0;
				  break;
				case SDLK_s:
					downs = 0;
				  break;
				case SDLK_ESCAPE:
					running = 0;
				  break;
				default:
				  break;
			}
		}
	}*/
	
	if(keystates[SDLK_a] && (dest.x > 0)){dest.x = (dest.x - 1);}
	if(keystates[SDLK_d] && (dest.x < 780)){dest.x = (dest.x + 1);}
	if(keystates[SDLK_w] && (dest.y > 50)){dest.y = (dest.y - 1);}
	if(keystates[SDLK_s] && (dest.y < 530)){dest.y = (dest.y + 1);}
 
	if(keystates[SDLK_j] && (dest2.x > 0)){dest2.x = (dest2.x - 1);}
	if(keystates[SDLK_l] && (dest2.x < 780)){dest2.x = (dest2.x + 1);}
	if(keystates[SDLK_i] && (dest2.y > 50)){dest2.y = (dest2.y - 1);}
	if(keystates[SDLK_k] && (dest2.y < 530)){dest2.y = (dest2.y + 1);}
	
	if(keystates[SDLK_ESCAPE]) running = 0;
}
[+][-]08/24/09 10:45 PM, ID: 25174793Expert Comment

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 30-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]08/24/09 11:00 PM, ID: 25174828Expert Comment

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 30-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]08/25/09 09:03 AM, ID: 25179006Accepted Solution

View this solution now by starting your 30-day free trial. Setting up your free trial is quick, easy, and secure. We will return you to this solution, unlocked, when you're done.

About this solution

Zones: Game Programming, Game Programming User Interface, C++ Programming Language
Tags: SDL Keyboard Key Presses Press Multiple Many Hardware
Sign Up Now!
Solution Provided By: maeve100
Participating Experts: 1
Solution Grade: A
 
[+][-]08/25/09 09:43 AM, ID: 25179464Expert Comment

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 30-day free trial to view this Expert Comment or ask the Experts your question.

 
 
Loading Advertisement...
20091111-EE-VQP-89 - Hierarchy / EE_QW_3_20080625