XNA - Keyboard Input

£可爱£侵袭症+ 提交于 2019-12-13 02:15:30

问题


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.


回答1:


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.



来源:https://stackoverflow.com/questions/3809246/xna-keyboard-input

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!