Search Unity

New input system, check if a key was pressed ?

Discussion in 'Input System' started by VincentAbert, Aug 16, 2020.

  1. VincentAbert

    VincentAbert

    Joined:
    May 2, 2020
    Posts:
    123
    Hello everyone !

    I'm trying to switch from the old the new input system, and I'm not a big fan of the action/callback style. I'm using a multi state controller using an enum, so I don't want to call a function whenever a button is pressed, but rather depending on the state he is in. I used to just use getbutton and the like, and it seems the new input system has similar functions like ispressed but all the examples use it by checking directly on the device ( is the 'space' key pressed ? ) And not the action map set up by the dev (is the jump button pressed ?)

    Is that possible ?

    Thank you !
     
  2. VincentAbert

    VincentAbert

    Joined:
    May 2, 2020
    Posts:
    123
    Okay so I figured out that you can use '.triggered' to check if the action was performed this frame... That's progress, but I still can't test wether the key was being held or released this frame.... I have spent so much time on this new input system to try and understand how to access the basic functions the old system had, it's so frustrating...
     
  3. Rene-Damm

    Rene-Damm

    Joined:
    Sep 15, 2012
    Posts:
    1,779
    The polling API for InputActions doesn't make this one nice ATM. Closest you can get ATM for both press and release detection is to have a Press interaction on the binding and set it to "Press and Release" and do something like

    Code (CSharp):
    1. var wasPressed = action.triggered && action.ReadValue<float>() > 0;
    2.  
    3. var wasReleased = action.triggered && action.ReadValue<float>() == default;
    Not great.

    The polling API still needs some more fleshing out. Adding IsPressed(), WasPressedThisFrame(), and WasReleasedThisFrame() methods to InputAction is in the works.
     
    tomjycUnity and WidmerNoel like this.
  4. VincentAbert

    VincentAbert

    Joined:
    May 2, 2020
    Posts:
    123
    I see, thank you, I had not figured out combining it with the read value, so I created two actions with the same keys, one for the pressing and one for the releasing. But to add a "is pressed" functionality I still need to create a bool that I toggle based on those inputs right ?

    That makes for some very complex code for simple stuff, I kinda regret switching, but I'll stick with it for now, IsPressed(), WasPressedThisFrame(), and WasReleasedThisFrame() should allow us to get the same functionality as the old system when they get there.
     
  5. Rene-Damm

    Rene-Damm

    Joined:
    Sep 15, 2012
    Posts:
    1,779
    With the Press interaction on there and it set to "Press and Release", there's no need for the extra bool. The action performing with a value of 1 implies "pressed this frame" and it performing with a value of 0 implies "released this frame".
     
  6. VincentAbert

    VincentAbert

    Joined:
    May 2, 2020
    Posts:
    123
    Ooooh I see, clever ! thanks for the help :)

    I do have another question with the Input system, I'm using cursor warping to allow for gamepad users to select and press buttons in a menu, but it seems the warped cursor doesn't trigger events like 'onPointerEnter' and the like, which kind of makes it useless ? Unless there is setting I'm missing or a way to manually raycast from the mouse, but I can't find information on this...
    Maybe this isn't the place where I should ask that, anyway thanks for the help.
     
  7. Rene-Damm

    Rene-Damm

    Joined:
    Sep 15, 2012
    Posts:
    1,779
    Are you using VirtualMouseInput from the GamepadMouseCursor sample? UI input should work fine with it.

    Overall, if UI input is set up with InputSystemUIInputModule and the Mouse device the warping happens on is tied to the "point" action of the input module, then warping of the cursor should come through just fine (it'll be a frame late but should come through).
     
  8. VincentAbert

    VincentAbert

    Joined:
    May 2, 2020
    Posts:
    123
    "VirtualMouseInput from the GamepadMouseCursor sample" - I am not sure I understand what that is, and I can't find docs with a google search. But I am using the InputSystemUIInputModule with the default settings for now.
    The warping does come through and works as expected, but when the cursor is over a button it does not highlight like it does with the mouse.
     
  9. samlletas

    samlletas

    Joined:
    Jan 2, 2016
    Posts:
    30
    If anyone else runs into this, you can also create extension methods while we wait for this to be officially implemented.

    1. Based on @Rene-Damm example, create the following class:
    Code (CSharp):
    1. public static class InputActionExtensions
    2. {
    3.     public static bool IsPressed(this InputAction inputAction)
    4.     {
    5.         return inputAction.ReadValue<float>() > 0f;
    6.     }
    7.  
    8.     public static bool WasPressedThisFrame(this InputAction inputAction)
    9.     {
    10.         return inputAction.triggered && inputAction.ReadValue<float>() > 0f;
    11.     }
    12.  
    13.     public static bool WasReleasedThisFrame(this InputAction inputAction)
    14.     {
    15.         return inputAction.triggered && inputAction.ReadValue<float>() == 0f;
    16.     }
    17. }
    2. Then in your code you can just do:
    Code (CSharp):
    1. if (action.WasPressedThisFrame())
    2. {
    3.     // Do stuff
    4. }
    For this to work don't forget to add the "Press and Release" interaction in the action settings.
     
    Last edited: Oct 2, 2020
  10. RunninglVlan

    RunninglVlan

    Joined:
    Nov 6, 2018
    Posts:
    182
    As I tested, "Press and Release" interaction is required only for
    wasReleasedThisFrame
    functionality.
    isPressed
    and
    wasPressedThisFrame
    work out of the box.
     
    samlletas likes this.
  11. GhAyoub

    GhAyoub

    Joined:
    Dec 7, 2018
    Posts:
    10
    Don't forget to use
    Code (CSharp):
    1. Mathf.Abs(inputAction.ReadValue<float>()) > 0f
    if your action supports negative values.
     
  12. Karsten

    Karsten

    Joined:
    Apr 8, 2012
    Posts:
    187
    Its fine that there is finally going on some change about Input in Unity, but I wish there was an out of the box setup
    mimic the old Input where you have gravity for axes and that stuff, this would also help to migrate very fast , the new Input system is rather huge and the learning curve is rather steep.
    "Clever is the enemy of Clear"....
     
    petey and athenspire like this.
  13. FileThirteen

    FileThirteen

    Joined:
    Oct 23, 2012
    Posts:
    40
    I created a paid asset that actually helps with the conversion of old Input Manager to new Input System. It let's you write code exactly the same as you're used to writing but leveraging the new Input System. There is a link in my signature. Basically it wraps the new Input System and tries to replicate the simplicity of the old system as much as possible. With the asset you can use a script written for the old Input Manager and make no code changes to it, replicate the Input Manager Settings in an Input Action Asset(included in the asset), and your code will just work with the new system. Though to your point Karsten, it doesn't provide the really advanced stuff that the new Input System provides like Hold Interactions and Tap Interactions and stuff like that. It does make your code really clean though since you don't have to deal with any callback methods or the like.
     
  14. mikeohc

    mikeohc

    Joined:
    Jul 1, 2020
    Posts:
    215
    Is there a roadmap for when this will be implemented?
     
  15. rryba

    rryba

    Joined:
    Feb 21, 2017
    Posts:
    8
    I've just switched to it because of some packaged I wanna try, and I am shocked how you can badly design it , it's learning curve is so high and setup everything is on some other places, it's garbage , and then I find out it's not even easily backwards compatible.
     
  16. ASPLADMIN

    ASPLADMIN

    Joined:
    Sep 8, 2016
    Posts:
    6
    Easy implementation with Action type = Button, Interaction added = Press with Press and Release Trigger Behavior.

    Code is as given below:

    Code (CSharp):
    1. private void OnFire (InputAction.CallbackContext context)
    2. {
    3.     if (context.action.triggered && context.action.ReadValue<float>() != 0 && context.action.phase == InputActionPhase.Performed)
    4.     {
    5.         //Perform Trigger Pressed Actions
    6.         TriggerPressed();
    7.     }
    8.     else if (context.action.triggered && context.action.ReadValue<float>() == default && context.action.phase == InputActionPhase.Performed)
    9.     {
    10.         //Perform Trigger Release Actions
    11.         TriggerReleased();
    12.     }
    13.  }
    14. private void TriggerPressed()
    15. {
    16.     //Trigger Pressed Actions
    17. }
    18. private void TriggerReleased()
    19. {
    20.     //Trigger Released Actions
    21. }
     
    Last edited: Sep 5, 2021
  17. Rene-Damm

    Rene-Damm

    Joined:
    Sep 15, 2012
    Posts:
    1,779
    In 1.1

    Code (CSharp):
    1. action.IsPressed()
    2. action.WasPressedThisFrame()
    3. action.WasReleasedThisFrame()
     
  18. samlletas

    samlletas

    Joined:
    Jan 2, 2016
    Posts:
    30
    Nice, and "Press and Release" interaction is not needed anymore for "WasReleasedThisFrame" to work.

    Though for some weird reason the package manager UI was not prompting me to update to the new version (I was still in 1.0.2), but was able to update after manually changing the version number in my manifest.json.
     
  19. Flemming-DC

    Flemming-DC

    Joined:
    Nov 29, 2020
    Posts:
    1
    When I go to the package manager, then I can't update the Input System to version 1.1, since it is marked as fully updated, even though it's just 1.0.2. Any help? I would really like to have the action.IsPressed() function.

    ps: I tried the trick that SamMacedo mentioned (manually change the version in manifest.json), but it didn't work for me.
     
  20. pragmascript

    pragmascript

    Joined:
    Dec 31, 2010
    Posts:
    107
    I have the same problem
     
  21. pragmascript likes this.
  22. stevphie123

    stevphie123

    Joined:
    Mar 24, 2021
    Posts:
    82
    I just want to, HOLY JESUS!

    Why is it so hard to do for such simple thing!?

    Bring the old workflow to the table!
     
    drewjosh likes this.
  23. xkittyx

    xkittyx

    Joined:
    Mar 10, 2022
    Posts:
    1
    I started learning Unity 2 days ago and I'm working on the first project, thought it would be cool to add a jump feature to the player but I've no idea how to add space key input in this new input system
     
  24. zarriel

    zarriel

    Joined:
    Sep 11, 2021
    Posts:
    1
    First of all, it's old topic. Make a new one.
    Second, I began coding last 4 months and I suggest you, go in old input system first. It's more simpler and clear. Try Input.GetKey(KeyCode.keyName) then Input.GetButton / ButtonUp / ButtonDown("axis action"). When you understand those 2 really good, try new Input, otherwise you'll just get discouraged.

    If you really want to start with new Input, at begin do it with:
    Keyboard.current.spaceKey.wasPressedThisFrame / isPressed / wasRelesedThisFrame
    or for mouse
    Mouse.current.leftButton.wasPressedThisFrame / isPressed / wasRelesedThisFrame

    And then move on with the correct Input. For new Input system don't forget to add to script using UnityEngine.InputSystem;

    Here is also complex and easy to understand knowledge about new Input system
    https://gamedevbeginner.com/input-in-unity-made-easy-complete-guide-to-the-new-system/
     
    Last edited: Apr 13, 2022
    betomaluje and CodeFriendlyArt like this.
  25. samyglancey

    samyglancey

    Joined:
    Oct 4, 2021
    Posts:
    2
    upload_2024-3-20_18-23-54.png

    For anyone who wants an easy way out...
     
  26. ludos

    ludos

    Joined:
    Nov 14, 2011
    Posts:
    56
    because this is the first hit when i look up this problem, this should work:

    Code (CSharp):
    1.  
    2. using UnityEngine.InputSystem;
    3.  
    4. ...
    5.  
    6.     void Update()
    7.     {
    8.         if (Keyboard.current[Key.Space].wasPressedThisFrame) {
    9.             Debug.Log("Key Pressed space");
    10.         }
    11.     }
    12.