Processing multiply keys

I'm trying to make a game using C++ and OpenGL and I'm having issues processing more than 1 key state at a time.

I tried
1
2
3
4
5
6
7
8
9
10
11
12
13
14
switch (wParam)
{
	case VK_LEFT:
		if (xPos > 48)
			xPos -= 4;
		break;

	case 0x58:
		mXpos = xPos;
		mYpos = yPos;
		shootMissile = true;
		break;
}
break;


Which should make the object move left and then '0x58' is the x key, but once pressed it would stop moving left.

and I tried using GetAsyncKeyState which did the same effect.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
if (GetAsyncKeyState(0x25))
{
	if (xPos > 48)
	        xPos -= 4;
}

if (GetAsyncKeyState(0x58))
{
	if (xPos > 48)
		xPos -= 4;

	mXpos = xPos;
	mYpos = yPos;
	shootMissile = true;
}


I'm doing them both in the Win32 call back function under WM_KEYDOWN:

Any ideas on how to make this work simultaneously?
closed account (DSLq5Di1)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
        static bool keyIsDown[256];
        ...

	case WM_KEYDOWN:
		if ((lParam & 0x40000000) == 0) // Skip repeated keystrokes, see MSDN for details.
			keyIsDown[wParam] = true;

		if (keyIsDown[VK_LEFT])
		{
			...
		}
		if (keyIsDown['X'])
		{
			...
		}
		break;

	case WM_KEYUP:
		keyIsDown[wParam] = false;
		break;

Not the most elegant solution, though I'm not sure what alternatives you have, other than using a library that handles input (DirectInput comes to mind).
Come on OP, the problem is your break statement. It isn't mandatory to put a "break" in every switch case block, but think about why we do it. It's because usually once a condition in a switch case block is met there is no reason to evaluate the other cases. But that's not true in this instance is it? Here you want to keep evaluating the other cases because, as your problem indicates, more then one key at a time might be pressed and your program needs to know to react to it.
Thanks for both the replies but I managed to fix it anyway, and I didn't think about the break statement at the time. And surely if I just remove the break statement it will just do the next part in the switch?

But nevermind about it, I done it
Topic archived. No new replies allowed.