开发者

XNA - Keyboard Input

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 cou开发者_C百科pled with XNA is a little confusing so, please be specific and give examples. Thanks.


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜