New Input System - how do I get what key is being pressed?

I don’t know if this has been asked before, but I started learning the new Input-System today. What I’m interested in knowing is:

How I would get the name of the key/button that is currently being pressed?

(Such as left and right for the horizontal inputs). Is there a way in the system that can be used to accomplish this?

This may help:

However, as the thread says its inefficient and just checking to see if a single key is pressed would be more efficient.
Cheers

That thread linked to by Harry Drew is an abomination… You will find only regret. It’s all for the old input system too, which is probably why it’s such a disaster.

After giving up on finding a good solution through web search (which lead me only here), I just guessed my way to how you should actually be doing it. It was easy to find through trial and error.

There are like a dozen different ways you can handle inputs with the new system, but all the good ones will utilize events or messages which will provide you with ‘context’. If you’re not using the callback context, you’re not using the new input system, it’s as simple as that.

context.control.displayName ← that’s what you need, that’s how you display the key that’s pressed.

Here’s example code to show you how I’m currently doing it.

private InputSystem_Actions playerInput;
private Vector2 wasdInput;

private void Awake()
{
    playerInput = new InputSystem_Actions();
}

private void OnEnable()
{
    playerInput.Enable();
    playerInput.Player.Move.performed += Move;
}

private void OnDisable()
{
    playerInput.Disable();
    playerInput.Player.Move.performed -= Move;
}

private void Move(InputAction.CallbackContext context)
{
    wasdInput = context.action.ReadValue<Vector2>;
    print(context.control.displayName);
}

The example above shows one of many ways to get the callback context from a keypress event. There are simpler ways, there are more complex ways; this is a decent one, a good way to use the new input system that I would recommend.

One of the key benefits of the new input system is that using events like these you don’t have to check every frame if a key was pressed, you just get the event sent when the key is pressed instead which can potentially save you a lot of resources that would have gone into checking for inputs with the old input system :nauseated_face:.

The bit in that code you’re looking for ultimately is print(context.control.displayName);

It prints the displayname of the key that was pressed to the console. Looks like this:

If you need a more robust way to check what key was pressed when any key was pressed, you can do this:

This code doesn’t work with the Tab key, but works with other buttons as far as I have tested:

using UnityEngine;
using UnityEngine.InputSystem;
using System.Collections.Generic;

public class PrintInput : MonoBehaviour
{
	public string binding = "/*/<button>";
	public List<string> excludeDisplayNames = new List<string>(new string[] {"Press", "Any Key", "Control", "Shift", "Alt"});

	void Start ()
	{
		InputAction anyInputAction = new InputAction(binding: binding);
		anyInputAction.performed += OnInput;
		anyInputAction.Enable();
	}

	void OnInput (InputAction.CallbackContext context)
	{
		string displayName = context.control.displayName;
		if (!excludeDisplayNames.Contains(displayName))
			print(displayName);
	}
}