# GetKeyState problem

This topic is 5047 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

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);

}



##### Share on other sites
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).

##### Share on other sites
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.

##### Share on other sites
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 =-)

1. 1
2. 2
Rutin
21
3. 3
4. 4
frob
17
5. 5

• 9
• 12
• 9
• 33
• 13
• ### Forum Statistics

• Total Topics
632591
• Total Posts
3007251

×