Microsoft.Xna.Framework.Input.Keyboard
Hiya Everyone,
Any advice on the following would be greatly appreciated.
I’ve developed a very basic static KeyboardHandler class, for the sole purpose of generating OnKeyDown events whenever some keys are being pressed.
The class has a member function called Update, which is called each time the Game.Update() member function is called i.e. it is called each time the game components are being updated.
The KeyboardHandler.Update() queries the KeyboardState to determine which keys have been pressed, and raises an event for each of the keys returned by the GetPressedKeys() method
I’ve noticed though that the following occurs:
1)The Keyboard.GetState().GetPressedKeys() is returning mouse button click events
Is this suppose to happen?
2)Pressing a Keyboard key once results in the same keyboard event being raised several times...
It seems like the GetPressedKeys() method is returning the same data as it did when it was queried before. This happens for approx. 8 iterations resulting in incorrect events being generated
(The call to GetPressedKeys() does not correctly flush when being called in high frequency - Timing these iterations seem like the keyboard actual state is only being refreshed every 1/10 of a sec).
Has anyone else experienced this?
Is there a way in which I can force a refresh of the Keyboard’ state i.e. make sure that the GetPressedKeys() returns no information if a key was not actually pressed?
Thanks
1) Don't know if its supposed to happen, but it happens to me too. I didn't even think to look there in my own input event reciever class.
2) It will be raised at every update unless you store the previous state and compare the two. In my class, I store the last keyboardState and compare it to the current keyboard state to see which keys were pressed in this update call. And then I raise the event for only the new keys of the current keyboardState. Then I replace old keyboardState with the current keyboardState.
Hiya Jim,
Thanks for the response.
With regards to ur second answer:
I thought of that as being a possible work around, but my only concern at the time was:
- How does one detect when a key is being held down (In that event the OnKeyDown event should be raised several times i.e. for as long as the key is being held down)?
Is the solution here to force clear the previously stored keyboard state every 1/10 of a second (or what ever frequency might do the trick)?
That way the system would try to compare the current keyboard state with the previous stored state, and instead of suppressing the keydown event; the Keydown event will be raised due to the previous state buffer being cleared.
I use a KeyDown and KeyUp events from my input event receiver. So example, if Space was for shooting. On KeyDown event of Space, then I'd set a (in my game class not input class) bool of shooting=true, then on KeyUp event, I'd set it to false. Then in my game class code (not input class), if shooting was true, then it was either clicked or being pressed down.
I've created some very helpful utility classes to manipulate the keyboard state (as well as mouse and gamepads) that I think may be of use to you.
I have the code posted on my site, which you can find my site through my public profile. The site doesn't have much on it at the moment so it'll be easy to find the "ThreeSixBox Utility Library".
In particular, you should look for the KeyboardHelper.HoldingKey() method.
I hope that helps.