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!

4 Answers

4

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;
        }
    });
}

maybe not a solution, it runs even when the game is not running. oops.

It's because you're adding a callback to the InputSystem, on any button pressed, on each frame. You should really only add that callback in Start!

Oh, thanks! Not sure if the whole code will work in start but I'll try.

It worked, thank you so much!

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)

Thanks! I currently am on Unity version 2022.3.3f1 (should've included that sorry).

All good, just needed to check so I could get up my projects

@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.

Thanks! Unfortunately that's not exactly what I'm looking for. I believe that's what the old input system uses, but for me I want to be able to use the new input system to work with any controller. The issue arises in finding what action (like if block or attack) is being performed, not what key is being pressed. But thanks anyway!

Ideally, I want it so I could do something like this: if(any input is performed) { Debug.Log(input action name) } Not sure how to describe it well.

@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

Very sorry for the back and forth messages. I hope this is what you want. :smiley:

Thanks for the help anyway! It's quite the difficult thing to find (I don't think it directly is a feature for what I want) but I've found something that potentially could work with some more code! Thanks, have a good one.

Good Job finding a solution, I hope it gets you what you want!