Skip to main content
GameDev.net gamedev.net
🔒 Locked

GetKeyState problem

Started by Tybon Nov 25, 2004 at 3:52 PM 2 replies 2.1k views
Original Post
Tybon
Tybon
Hi. I'm having trouble with GetKeyState when multiple keys are pressed down. For example, I use the arrow keys to control the camera movement, as shown below. If I have both up arrow and down arrow keys pressed down, then the left arrow key doesn't work, but strangely the right arrow key works. Similarly, if I have both left and right arrow keys pressed down, then the up arrow key doesn't work but the down arrow key works. I'm not sure how GetKeyState works internally, but there seems to be something wrong when multiple keys are pressed. Any help is appreciated!

void get_keyboard_input()
{
	if(GetKeyState(VK_UP) & 0x80)
		Camera.MoveCamera(MOVE_FORWARD);

	if(GetKeyState(VK_DOWN) & 0x80)
		Camera.MoveCamera(MOVE_BACKWARD);

	if(GetKeyState(VK_RIGHT) & 0x80)
		Camera.MoveCamera(MOVE_RIGHT);

	if(GetKeyState(VK_LEFT) & 0x80)
		Camera.MoveCamera(MOVE_LEFT);

}

b2b3
b2b3
Maybe you can try GetAsyncKeyState state which is not dependent on messages sent to your window. You should watch whether your window is active or not then, but that should not be problem (just create some variable which is toggled when window gets/loses focus).
Tybon
Tybon
I have tried GetAsyncKeyState but the same thing happens... Anyways GetKeyState functions seem pretty weak so I think I'll just learn how to use direct input and see if it works better. Thanks.
Extrarius
Extrarius
You could try GetKeyboardState, and then index that array instead of calling a function for every key. It might not fix your problem, but it is easy to try while DirectInput isn't quite so simple and wouldn't be needed if this fixes your problem =-)
"Walk not the trodden path, for it has borne it's burden." -John, Flying Monk

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.