Detecting & Returning any key pressed in New Input System (2D)

Hi! First time using this. In my game, I’m trying to figure out a way to detect any key pressed and see what it is via Unity’s new Input-System. My plan is to add it to a list eventually, but that isn’t the issue.

I essentially want it so when I press an action, - let’s just say attack, for it to read that on any device, then print via Debug.Log for now. The catch comes in me trying to have it more robust, as for I want it to be able to detect it for ANY action, not just individually writing code for each action. Worst case, I manually do that, but I thought I just may ask the forums before turning my code into a big 'ol mess.

Thanks!

I should be able to help, I just need to know what unity version you are using. I am also creating a 2D game right now too!

If you don’t know how to find the version, open unity hub and go to the project tab and it should show the version of your project. (I can’t get an image right now, I am on my phone)

@monkeyman23

Ok, I have found my 2d project and you detect keys like this:

if (Input.Getkey("w")) {
Debug.Log("The w key has been pressed!");
}
if (Input.Getkey("a")) {
Debug.Log("The a key has been pressed!");
}
if (Input.Getkey("s")) {
Debug.Log("The s key has been pressed!");
}
if (Input.Getkey("d")) {
Debug.Log("The d key has been pressed!");

This code is placed in a loop so that it detects the keys WASD. You can change the letters to suit you game too. Hope this helps! Contact me if there are any problems or questions!

Ps. Do you want multiple keys to do the same thing. I can’t properly understand what you said the problem was.

@monkeyman23

Ok, you are talking about the new 1.8.1 input system which you have to install as a unity package which is available in the package manager. I don’t know how to use it though, so you could flag this post or look at the page for it here:
https://docs.unity3d.com/Packages/com.unity.inputsystem@1.8/manual/index.html

This worked for me okay! Not exaclty what I wanted, but this does fetch the input pressed (not the action associated, but that’s fine). The only issue is the Debug.Log will tank your frames BIG time, so just comment it out unless needed. Hope it helps anyone else needing this!

PS, I just realized the buttonPress doesn’t make it fire once, so just don’t include that or fix it, sorry!

void Update()
{
    InputSystem.onAnyButtonPress.Call(currentAction =>
    {
        buttonPress = true; 
        if (currentAction is ButtonControl button && buttonPress)
        {    
            //ONLY USE THIS DEBUG FOR TROUBLESHOOTING, CAUSES BIG LAG.
            //Debug.Log($"Key {currentAction.name} pressed! (text: {currentAction.displayName})");
            buttonPress = false;
        }
    });
}