Detect If Certain Key Was Pressed Twice Rapidly
c#, xna
Solution
Paul_R and Nikola have actually answered the question for you, but since you said you're new to XNA (read: haven't read all the docs) allow me to introduce some important classes:
We know that a keyboard basically can trigger two types of events: `KeyDown` (on press before release) and `KeyUp` (on release).
A key is pressed when a `KeyDown` event and a `KeyUp` event occur consecutively. I usually store 2 states for this:
private KeyboardState previousKeyboardState;
private KeyboardState currentKeyboardState;
Make sure you also import the necessary package to use these classes.
using Microsoft.Xna.Framework.Input;
Although probably inefficient since you can just have a listener, I update those two states in the main game loop:
protected override void Update(GameTime gameTime){
previousKeyboardState = currentKeyboardState;
currentKeyboardState = Keyboard.GetState();
}
If I want to know if a key is pressed, I just call this function:
public bool isKeyPressed( Keys key ){
return previousKeyboardState.IsKeyDown(key) && currentKeyboardState.IsKeyUp(key);
}
`Keys` is an enum. It contains an enumeration of keyboard keys. For example the letter A in the keyboard is represented by `Keys.A`. The keyboard key PageDown is `Keys.PageDown`. For instance, I want to know if the letter B was pressed, I just call the above method using this statement:
isKeyPressed( Keys.B ); //returns true or false
Now we know if a key is pressed. This is where your question comes in: how do we know if a key was pressed twice rapidly. You need to define what rapidly is. For this example, lets say a rapid press on a key occurs when a key is pressed within 1000 milliseconds after its first press.
Recall that your main game loop receives a parameter `GameTime gameTime.` You can use this, as suggested by Paul_R, to get the elapsed time between the previous update and the last update using this statement:
elapsedTime = gameTime.ElapsedGameTime.TotalMilliseconds;
Noticed that we used the `Milliseconds` property, not the `Seconds`. This is just for consistency's sake. `gameTime.ElapsedGameTime.TotalMilliseconds` is the amount of elapsed game time in milliseconds since the last update.
This isn't really an elegant way to do this, but just so you can get the idea, here's a method I made to check if a key was pressed rapidly:
public bool isKeyPressedRapidly( Keys key ){
return isKeyPressed(key) && elapsedTime <= 1000 && lastKeyPressed == key;
}
`elapsedTime` is reset everytime `isKeyPressed(key)` returns `true`. Increment `elapsedTime` in the main game loop until another key is pressed. The value of `lastKeyPressed` is, well, the last key you pressed. I'll leave the rest to you. Cheers and have fun!
Problem
I need to check whether or not the "D" or "A" key was pressed twice rapidly, in order for the sprite to change and animate. Once that key is let go, the sprite should go back to it's original form. I have a stick figure that once either key is pressed, it should duck down and begin to "roll" in either direction, depending on which key was pressed. Any help would be appreciated!