XNA - 键盘输入

发布于 2024-09-25 19:23:35 字数 127 浏览 0 评论 0原文

我今天刚刚开始使用 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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

绳情 2024-10-02 19:23:35

如果您习惯在 VS 中使用对象浏览器,我建议您查看 Microsoft.Xna.Framework.Input.Keyboard/Keyboardstate。这些条目将向您展示您可以使用的现成功能。或者,您可以查看 MSDN 或按照 Creator's Club 上的教程进行操作。我将发布一个快速片段来检查特定的击键。

currentState = Keyboard.GetState();

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

previousState = currentState;

theKey 是在此代码段范围之外定义的参数。您可以将 theKey 设置为一个特定值,您希望在按下该值时触发某些特定的程序行为(在上面片段中的注释位置)。 theKey 定义为:

Keys theKey

previousState 和 currentState 定义为:

private static KeyboardState currentState;
private static KeyboardState previousState;

虽然可能不是最漂亮的方法,但它有效,并且是一个相当简单的构建示例。希望有帮助。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文