Unity New Input System - Get Keyboard Keys

Here is a good way to get keyboard keystrokes in Unity using the New Input System.

First set up the Input Actions as shown in the image bellow for each letter:


Finally, setup the following code in your class:

MyControls _inputActions;
InputAction _keyboardLettersInputAction; 

private void OnEnable()
{
     if (_inputActions == null) _inputActions = new MyControls();
    _keyboardLettersInputAction = _inputActions.Game.KeyboardLetters;
    _keyboardLettersInputAction.Enable();
}

bool _blocked;

private void CheckKeyboardInputActionOnUpdate()
{
    if (!_keyboardLettersInputAction.IsPressed())
    {
        _blocked = false;
        return;
    }

    if (_blocked) return;
    _blocked = true;

    char letter = char.ToUpper(_keyboardLettersInputAction.activeControl.name[0]);
    Debug.Log(letter);

    _keyboardLettersInputAction.Reset();
}

private void Update()
{
    CheckKeyboardInputActionOnUpdate();
}


Comments