Reputation: 321
I am trying to check if a key is pressed on the keyboard with Xna.Framwork.Input, but the method i use doesn't see to work.
My code (only important parts):
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);
}
No matter what I press, "IsKeyPressed" never returns true.
This method is executed in a new Thread, but running it in the main thread didn't change anything.
I am using Visual Studio 2012 Ultimate Update 1, .NET 4.5, Win 7 x64, XNA Game Studio 4.0
Is my code incorrect or is the problem somewhere else?
Upvotes: 0
Views: 829
Reputation: 5762
Input is not threadable, maybe due to the windows message loop is in the main thread,
if you have no other constrain, you should use the game class that xna provides, and invoke Input.GetState inside the update method... after you can use the keyboarstate in the thread that you want.
Upvotes: 1