0
KeyboardState kbstate = Keyboard.GetState();
Keys[] pressed = kbstate.GetPressedKeys(); 

EnterEscBackspaceAltCtrlWinNumlockHomeIns and possibly more worked fine but when I press any letter, number, or arrows, it won't read it.

4

1 回答 1

1

I have only ever seen keyboard input handled like so:

KeyboardState kbState = Keyboard.GetState();

if (kbState.IsKeyDown(Keys.A))
{
    // 'A' key is down
}

If you wanted key pressed (i.e. the button was just pressed) you would use the following method:

public bool IsNewKeyPress(Keys key)
{
    return (kbState.IsKeyDown(key) &&
         oldKbState.IsKeyUp(key));
}

// And in the update method...
public void Update(GameTime gameTime)
{
    oldKbState = kbState;
    kbState = Keyboard.GetState();

    if (IsNewKeyPress(Keys.A))
    {
        // A was *just* pressed
    }

    // ...
}
于 2012-09-22T23:38:10.333 回答