XNA-键盘输入 [英] XNA - Keyboard Input
问题描述
我今天才刚开始使用XNA Framework 4.0,我想知道最简单的方法是从键盘获取输入.我在C#中认识到很多C ++,但是它的整个Java方面对我来说都是陌生的.再加上XNA,这有点令人困惑,因此请具体说明并举一些例子.谢谢.
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.
推荐答案
如果您对VS中的对象浏览器感到不满意,建议您查看Microsoft.Xna.Framework.Input.Keyboard/Keyboardstate.这些条目将向您显示现成功能方面的可用功能.另外,您可以查看MSDN或遵循Creator's Club上的教程.我将发布一个简短的代码片段,以检查特定的击键.
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是在此代码段范围之外定义的参数.您可以将Key设置为一个特定的值,您希望在按下该键时触发特定的程序行为(在上面片段中的注释位置). theKey定义为:
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和currentState定义为:
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.
这篇关于XNA-键盘输入的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!