GetState.IsKeyDown()永远不会返回true(Microsoft.Xna.Framework.Input.Keyboard)
问题描述:
我想检查一个键是否与Xna.Framwork.Input键盘上按下,但我使用的方法不看到工作。GetState.IsKeyDown()永远不会返回true(Microsoft.Xna.Framework.Input.Keyboard)
我的代码(仅重要部分):
using System.Threading;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework;
private static void GetInput() {
for (int i = 0; i < 1000; i++) {
Update();
if (IsKeyPressed(Keys.W)) {
Console.WriteLine("W pressed");
}
if (IsKeyPressed(Keys.S)) {
Console.WriteLine("S pressed");
}
Thread.Sleep(10);
}
}
public static KeyboardState CurrentKeyboardState { get; private set; }
public static void Update() {
CurrentKeyboardState = Keyboard.GetState();
}
public static bool IsKeyPressed(Keys key) {
return CurrentKeyboardState.IsKeyDown(key);
}
不管我怎么按, “IsKeyPressed” 永不返回true。
该方法在一个新的线程中执行,但在主线程中运行它并没有改变任何东西。
我使用Visual Studio 2012旗舰版更新1,.NET 4.5,Win 7的64,XNA游戏工作室4.0
是我的代码不正确或有问题的其他地方?
答
输入不是螺,可能是由于Windows消息循环是在主线程,
,如果你没有其他的约束,你应该使用游戏类XNA提供,并调用Input.GetState内更新方法...之后,您可以在所需的线程中使用keyboarstate。