views:

40

answers:

2

I just started using XNA Framework 4.0 today, and I was wondering what the easiest way was to get input from the keyboard. I recognize a lot of C++ in C# but the whole Java side of it is alien to me. This coupled with XNA is a little confusing so, please be specific and give examples. Thanks.

+2  A: 

This tutorial explains it.

Andrew Russell
+1  A: 

If you're comfortable mucking around with the Object Browser in VS, I'd advise looking at Microsoft.Xna.Framework.Input.Keyboard/Keyboardstate. These entries will show you what you have available to you in terms of ready-made functions. Alternatively, you could look on MSDN or follow a tutorial on Creator's Club. I'll post a quick snippet that checks for a specific keystroke.

currentState = Keyboard.GetState();

if(currentState.IsKeyDown(theKey) && previousState.IsKeyUp(theKey))
{
   //Do something here
}

previousState = currentState;

theKey is a parameter that is defined outside of the scope of this snippet. You could set theKey to a specific value that you would want to trigger some specific program behavior on pressing (at the commented location in the fragment above). theKey is defined as:

Keys theKey

previousState and currentState are defined as:

private static KeyboardState currentState;
private static KeyboardState previousState;

While perhaps not the prettiest way of doing that, it works and is a fairly straightforward example to build from. Hope that helps.

Rich Hoffman
That really did help. Thanks :D. I eventually found out how to do this and did it the exact same way. However what you showed me actually helped me optimize my code. Thanks again.
Lemmons