Docs / MonoInput

MonoInput: keyboard

Checking a key by its state, Ctrl, Shift and Alt, shortcuts, every key held at once, a key-binding screen, typed text, and updating from the keyboard or from a state of your own.

Color legend plain property method

KeyboardManager reads the keyboard at every update and answers for any key. It knows keys by the Keys value MonoGame names them with, and has no events: game code asks it. Names are colored by what they are: a plain property or a method.

Checking a key

Five methods ask about one key, one per check:

if (KeyboardManager.IsKeyPressed(Keys.Space)) Jump();     // once, at the press
if (KeyboardManager.IsKeyDown(Keys.Right)) MoveRight();   // every frame the key is held
if (KeyboardManager.IsKeyUp(Keys.E)) ThrowCharged();      // once, at the release

The checks read the state the last Update left, so every check in a frame agrees, wherever it is made. Until the first update, every key is idle.

IsKeyUp is not MonoGame’s KeyboardState.IsKeyUp. MonoGame’s is true for every key that isn’t held; this one is true only at the update the key was let go. For “not held”, use !IsKeyDown(key).

A check looks the key up in the list of keys being touched, which holds a few keys at a time, so checks are cheap and don’t allocate.

KeyboardManager checks

Method Description
IsKeyIdle(Keys key) Static. The key isn’t touched: it isn’t in ActiveKeys.
IsKeyPressed(Keys key) Static. The key went down at the last update: true for that update only.
IsKeyDown(Keys key) Static. The key is held: Pressed or Down, the update it went down included, as with MonoGame’s KeyboardState.IsKeyDown.
IsKeyUp(Keys key) Static. The key was let go at the last update: true for that update only.
IsKeyActive(Keys key) Static. The key is pressed, held or just let go: it is in ActiveKeys.

Modifiers and shortcuts

IsCtrlDown, IsShiftDown and IsAltDown take the left and the right key as one. Check the modifier with them and the key with IsKeyPressed, so the shortcut fires once:

if (KeyboardManager.IsCtrlDown && KeyboardManager.IsKeyPressed(Keys.S)) Save();

if (KeyboardManager.IsCtrlDown && KeyboardManager.IsKeyPressed(Keys.Z))
{
    if (KeyboardManager.IsShiftDown) Redo();
    else Undo();
}

// S alone, not as part of Ctrl+S
if (KeyboardManager.IsKeyPressed(Keys.S) && !KeyboardManager.IsCtrlDown) MoveBack();

The three are Down checks, so they turn false at the update the modifier is let go. Were they Active checks, true at the release too, Ctrl+S could fire when S goes down at the very update Ctrl comes up.

For one side only, check its key: KeyboardManager.IsKeyDown(Keys.LeftShift).

KeyboardManager modifier properties

Property Type Description
IsCtrlDown bool Static, read-only. Left or right Control is held. False from the update it is let go.
IsShiftDown bool Static, read-only. Left or right Shift is held. False from the update it is let go.
IsAltDown bool Static, read-only. Left or right Alt is held. False from the update it is let go.

Every key held

ActiveKeys lists every key that isn’t idle: pressed, held or just let go, in the order they were first pressed. Each item is a KeyStatus with the Key and its State:

// the keys on screen, for a tutorial or a debug overlay
var y = 10;
foreach (var status in KeyboardManager.ActiveKeys)
{
    var color = status.State == ButtonStates.Up ? Color.Gray : Color.White;
    _spriteBatch.DrawString(_font, status.Key.ToString(), new Vector2(10, y), color);
    y += 20;
}

The list is a live view, the same object for the whole run, and foreach over it doesn’t allocate. Don’t hold on to a status: once its key is idle it leaves the list, stays Up and is never updated again. The button states page has more on the lists.

How many keys a keyboard reports at once is up to the keyboard: many can’t report some combinations of three or more keys, and the ones they leave out are simply not held, as far as the game can tell.

KeyboardManager active keys

Property Type Description
ActiveKeys ActiveInputs<KeyStatus> Static, read-only. Every key that isn’t idle, in the order they were first pressed: a live view, the same object for the whole run.

KeyStatus members

Member Description
Key Read-only Keys. The key, as MonoGame names it.
State Read-only ButtonStates, from ActiveInputStatus<TId>. Pressed, Down or Up.
KeyStatus(Keys key) A status for the key, Pressed. The keyboard manager makes its own; one you create is never updated.

A key-binding screen

To let the player choose a key, wait for the next key pressed:

// in Update, while the screen waits for the key of "Jump"
foreach (var status in KeyboardManager.ActiveKeys)
{
    if (status.State != ButtonStates.Pressed) continue;

    if (status.Key != Keys.Escape) _bindings.Jump = status.Key; // Escape cancels
    _waitingForKey = false;
    break;
}

// in the game, the key chosen
if (KeyboardManager.IsKeyPressed(_bindings.Jump)) _player.Jump();

Start waiting at the frame after the one that opened the screen. In that frame, the key that opened it, such as Enter, is Pressed too, and would be taken as the answer.

For actions with several bindings each, rebinding with conflicts and modifier keys, see the input binder, which MonoInput can feed.

Typed text

ActiveKeys reports keys, not characters. It doesn’t know the keyboard layout, Shift for capitals, dead keys, or the repeat of a key held down. For a text box, take the characters from MonoGame’s Window.TextInput event, which gives them as they were typed, and keep MonoInput for the keys that act, such as Enter to confirm and Escape to cancel. BorrowedTorch.UI’s text boxes work that way. A game in a window MonoGame didn’t create takes the characters from its host’s text events instead.

Updating

Update reads the keyboard with Keyboard.GetState() and moves every key one step: a key that went down is added Pressed, a held one becomes Down, a released one Up, and an Up one leaves the list. Call it once per frame, at the top of the game’s Update. It keeps a buffer for the keys held, so it doesn’t allocate once the buffer fits them.

Update(KeyboardState keyboardState) does the same over a state the game built, instead of reading the keyboard: the keys it holds are the keys held. It is for keys that never reach MonoGame’s Keyboard, such as an editor viewport’s in a window of its own, see feeding your own state, and for replays and tests:

// a replay: the keys held at each frame of the recording, played back
KeyboardManager.Update(new KeyboardState(_recording[_frame]));
_frame++;

_recording is a Keys[][] your own code filled, one array of held keys per frame. Call one of the two updates per frame, not both: they move the same keys.

KeyboardManager update methods

Method Description
Update() Static. Reads the keyboard and moves every key one step. Call it once per frame, before anything checks a key: a second call in the same frame moves the keys again, so a key just pressed reads Down.
Update(KeyboardState keyboardState) Static. The same, over a state the game built instead of the keyboard: the keys it holds are the keys held. Once per frame too, in place of Update().