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

Core Plot: How do I set the x-axis go through zero and at the bottom and y-axis to zero and left?

ios core-plot

I am a newbie to CorePlot and finally managed to get some scattered line plot display.

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.