Reputation:
I am having trouble with Keyboard.GetState() and .IsKeysDown(). They simply won't work.
Upvotes: 1
Views: 1571
Reputation: 879
It sounds like you may be experiencing a couple of issues. Since other new developers are likely to find this question when looking for help with Keyboard input I will try to cover both.
1) Keyboard.GetState() returns a snapshot of which keys are currently being held down. KeyboardState.IsKeyDown() does not identify keystrokes. It just tells you which keys are currently being pressed. Due to the way this works, it is best if you store the keyboard state before attempting to use it.
2) In your Update loop you will want to store the current keyboard state:
KeyboardState newKeyboardState = Keyboard.GetState();
Now you can check if a key press has occurred. A key press means that a key used to be down, and is now up:
if(previousKeyboardState.IsKeyDown(Keys.A && !newKeyboardState.IsKeyDown(Keys.A)) {
Console.Out.WriteLine("Keystroke: A");
}
Finally, save the current state so you can check against it later:
previousKeyboardState = newKeyboardState;
Upvotes: 0
Reputation:
If you have Synergy, then it might be one of the problems.
I believe it clears the keyboard input cache or something along the lines, so when you poll the hardware again Windows doesn't have any keystrokes to identify.
So when debugging in XNA, disable Synergy.
Upvotes: 1