Search Unity

  1. Unity Asset Manager is now available in public beta. Try it out now and join the conversation here in the forums.
    Dismiss Notice

Ubuntu based Linux - Unity 2021.1.0a4 - Input.GetKeyDown(KeyCode.Alpha#) no longer working

Discussion in '2021.1 Beta' started by Deleted User, Nov 6, 2020.

  1. Deleted User

    Deleted User

    Guest

    Any script using:
    Code (CSharp):
    1. Input.GetKeyDown(KeyCode.Alpha#)
    where Alpha# is replaced by Alpha0, Alpha1, Alpha2, Alpha3, Alpha4, Alpha5, Alpha6, Alpha7, Alpha8 and Alpha9 will not work.


    How to reproduce:
    • add the following script to a game object and press the numeric keys on the keyboard (not on the numpad) and see what is returned in the console.
    Code (CSharp):
    1.  
    2. using System.Collections;
    3. using System.Collections.Generic;
    4. using UnityEngine;
    5. using System;
    6. using System.Linq;
    7.  
    8. public class Test : MonoBehaviour
    9. {
    10.     private static readonly KeyCode[] KeyCodes = Enum.GetValues(typeof(KeyCode))
    11.         .Cast<KeyCode>()
    12.         .Where(k => ((int)k < (int)KeyCode.Mouse0))
    13.         .ToArray();
    14.  
    15.     private void Update()
    16.     {
    17.         if (Input.anyKeyDown)
    18.  
    19.             for (int i = 0; i < KeyCodes.Length; i++)
    20.             {
    21.                 KeyCode keycode = KeyCodes[i];
    22.                 if (Input.GetKeyDown(keycode))
    23.                 {
    24.                     Debug.Log($"Input detected: {keycode.ToString()}");
    25.                 }
    26.             }
    27.     }
    28. }
    29.  
    pressing any alpha numeric key should return the corresponding number 1 to 0; right now it returns "&" when I press the alpha numeric key 1 instead of 1, "é" when I press the alpha numeric key 2 instead of 2, and so on.

    The bug reporter not working (see: https://forum.unity.com/threads/ubuntu-based-linux-unity-2021-1-0a4-doesnt-start-maximized.1000501/), I cannot report the bug.

    Thank you for helping about that @LeonhardP! :)
     
    Last edited by a moderator: Jan 8, 2021
  2. Deleted User

    Deleted User

    Guest

    Same problem in 2021.1.0a5 and the bug reporter doesn't work either.
     
  3. LeonhardP

    LeonhardP

    Unity Technologies

    Joined:
    Jul 4, 2016
    Posts:
    3,135
    Deleted User likes this.
  4. Deleted User

    Deleted User

    Guest

    @LeonhardP Same thing in 2021.1.0a8. Bug reporter still not working.

    Capture d’écran_2020-12-05_09-37-11.png
     
  5. Deleted User

    Deleted User

    Guest

    So, how is the investigation going? :)

    The problem is still there in 2021.1.0a9.

    For info, @miniwolf_unity
     
  6. Deleted User

    Deleted User

    Guest

    So, where are you with this and could, at least, someone answer me?

    The problem is still happening in 2021.1.0b1.
     
  7. adamgolden

    adamgolden

    Joined:
    Jun 17, 2019
    Posts:
    1,555
    Just out of curiosity, does for example GetKeyDown("1") work even though KeyCode.Alpha1 doesn't?
     
  8. LeonhardP

    LeonhardP

    Unity Technologies

    Joined:
    Jul 4, 2016
    Posts:
    3,135
    Hi @APSchmidt,

    The issue with the bug reporter is currently being fixed and you can track the status of it here: https://issuetracker.unity3d.com/product/unity/issues/guid/1293315/

    As for the input issue, if you are pressing Left (or Right) Shift and hold it, then your code will never return the line "The alpha nine (9) key was pressed." Input.GetKeyDown returns TRUE only once per frame, so the nested ifs will both be TRUE only when both keys are pressed during the exact same frame. If the outer if statement had Input.GetKey instead, then Left (or Right) shift could be held and both keys wouldn't need to be pressed during the same frame. Then the line would be printed in the console.
     
  9. Deleted User

    Deleted User

    Guest

    I haven't tried but "1" being a string it should work only if you press the 1 key on the numpad (KeyCode.Keypad1 in C#). The string on the 1 key on the alphanumeric keyboard is "&" for me; it's probably different for other keyboards. :)

    When will you fix the problem? Printing 1 by pressing the 1 key on the alphanumeric keyboard should not require pressing the shift key beforehand.

    KeyCode.Alpha1 is still supposed to work, if you believe the documentation.

    Thank you for that. :)
     
  10. adamgolden

    adamgolden

    Joined:
    Jun 17, 2019
    Posts:
    1,555
    For me on Windows 10, KeyCode.Alpha1 and "1" both react to the 1 key above Q. Neither react to numpad 1 (whether NumLock is on or off).
     
  11. LeonhardP

    LeonhardP

    Unity Technologies

    Joined:
    Jul 4, 2016
    Posts:
    3,135
    The problem is with your code.

    You wrote
    but your code will only print to the console when shift is being pressed during the exact same frame Alpha1 is being pressed due to the nested if statements.
     
  12. adamgolden

    adamgolden

    Joined:
    Jun 17, 2019
    Posts:
    1,555
    What @LeonhardP is saying is you want to use GetKey with the shift keys.. i.e.:
    Code (CSharp):
    1. if (Input.GetKeyDown(KeyCode.Alpha1))
    2. {
    3.   if (Input.GetKey(KeyCode.LeftShift) || Input.GetKey(KeyCode.RightShift))
    4.   {
    5.     Debug.Log("The alpha one (1) key was pressed.");
    6.   }
    7. }
    ..otherwise, you would have to press the Alpha1 key AND a shift key in exactly the same frame.
     
  13. Deleted User

    Deleted User

    Guest

    Yeah, the problem doesn't happen in Windows, only in Linux.

    This is normal since the code for the 1 key on the numpad is KeyCode.Keypad1. I just tried GetKeyDown("1"), it doesn't work at all for me, whether I press Alpha1 or Keypad1. :)

    I've modified my first post and removed the unnecessary code; if you want to try it. If you can reproduce the problem, I would be grateful if you could handle the bug report. :)
     
    Last edited by a moderator: Jan 8, 2021
  14. LeonhardP

    LeonhardP

    Unity Technologies

    Joined:
    Jul 4, 2016
    Posts:
    3,135
    Looks like you're using the AZERTY keyboard layout.


    With that layout you need to hold shift to return numerals.
     
  15. Deleted User

    Deleted User

    Guest

    Since when? I never had to use the shift key and I've been using that keyboard for years. It all began with Unity 2021 Alpha, never happened with previous versions.

    Okay, it might be because we're looking for the string. What about trying the code with a real action? Like moving an object; normally, doing this should not require the use of the shift key, just the alpha numeric 1 (or whatever other).

    Off topic: Unity 2021 beta doesn't create the C# solutions; I'm using MonoDevelop. Up to 2021 Alpha, Unity created the solutions without any problem; no longer. Has this been reported yet or must I kiss goodbye to MonoDevelop and switch to Visual Studiio code? I'd rather not do that.
     
  16. Deleted User

    Deleted User

    Guest

    @LeonhardP Yes, that was the problem!

    Try the following code and see if you manage to reproduce the problem. The code should return the alpha string and disable the directional light on the scene. It works in 2020.2.1f1 and I don't need to press the shift key to disable the light:
    Code (CSharp):
    1. using UnityEngine;
    2. using System;
    3. using System.Linq;
    4.  
    5. public class Test : MonoBehaviour
    6. {
    7.     private GameObject dirLight;
    8.  
    9.     // Start is called before the first frame update
    10.     private void Start()
    11.     {
    12.         dirLight = GameObject.Find("Directional Light");
    13.     }
    14.  
    15.     private static readonly KeyCode[] KeyCodes = Enum.GetValues(typeof(KeyCode))
    16.         .Cast<KeyCode>()
    17.         .Where(k => ((int)k < (int)KeyCode.Mouse0))
    18.         .ToArray();
    19.  
    20.     // Update is called once per frame
    21.     private void Update()
    22.     {
    23.         if (Input.anyKeyDown)
    24.  
    25.             for (int i = 0; i < KeyCodes.Length; i++)
    26.             {
    27.                 KeyCode keycode = KeyCodes[i];
    28.                 if (Input.GetKeyDown(keycode))
    29.                 {
    30.                     Debug.Log($"Input detected: {keycode.ToString()}");
    31.                     dirLight.SetActive(false);
    32.                 }
    33.             }
    34.     }
    35. }
     
  17. Deleted User

    Deleted User

    Guest

    Okay, you won't be able to reproduce the problem with 2021.1.0b1 since the code is working there. I know for sure that it doesn't work in 2021 Alpha but I guess that reporting a bug is useless since 2021 is now out of Alpha. How does it work?

    Well, I needed a good news for today and I've got it. All this because of my lack of knowledge; sorry for the bother... :(

    And Happy New Year! :)
     
    adamgolden likes this.
  18. LeonhardP

    LeonhardP

    Unity Technologies

    Joined:
    Jul 4, 2016
    Posts:
    3,135
    Happy new year to you as well and I'm glad to hear it's working for you. If there's no issue (anymore), then there's also no need to report anything.

    We did check your code against a4 and couldn't find anything wrong. This also aligns with the returns you were seeing initially,
    Code (CSharp):
    1. Input detected: Alpha1
    2. UnityEngine.Debug:Log (object)
    3. Test:Update () (at Assets/Test.cs:32)
    and
    Code (CSharp):
    1. Alpha1 was pressed
    2. UnityEngine.Debug:Log (object)
    3. Test:Update () (at Assets/Test.cs:43)
    Alpha1 was detected but the log message wasn't displayed due to your use of GetKeyDown instead of GetKey.
     
    Deleted User likes this.
  19. LeonhardP

    LeonhardP

    Unity Technologies

    Joined:
    Jul 4, 2016
    Posts:
    3,135
    No need to apologize. Don't let that discourage you from reporting/discussing your findings.
     
    Deleted User likes this.
  20. Deleted User

    Deleted User

    Guest

    Unfortunately, not entirely, a very similar code doesn't work in 2021 beta and 2020.2.1f1.

    In the Swords and Shovel project, pressing an alpha numeric key equips the player with a sword and pressing the same key back unequips it. To have this work in that version, I need to use the shift key in combination with the alpha numeric key. In earlier versions, I don't need to use the shift key. Any idea why this is happening?

    The code is the following:
    Code (CSharp):
    1.     private void Update()
    2.     {
    3.         if (Input.GetKeyDown(KeyCode.Alpha1))
    4.             TriggerItemUse(101);
    5.         if (Input.GetKeyDown(KeyCode.Alpha2))
    6.             TriggerItemUse(102);
    7.         if (Input.GetKeyDown(KeyCode.Alpha3))
    8.             TriggerItemUse(103);
    9.         if (Input.GetKeyDown(KeyCode.Alpha4))
    10.             TriggerItemUse(104);
    11.     }
    Do you know anything about that?
     
    Last edited by a moderator: Jan 9, 2021