GetState.IsKeyDown() never returns true (Microsoft.Xna.Framework.Input.Keyboard)

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?

Answers


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.


Need Your Help

Finding out if an Expression contains a “lonely” ParameterExpression

c# expression

Is there a simple way of finding out if an Expression contains a ParameterExpression that isn't further wrapped in for example a MemberExpression.

Phone not connecting to eclipse since update to ADB 22

android windows eclipse adb galaxy

My Samsung Galaxy S2 is not connecting to eclipse after I updated the ADT to version 22.