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

Any script using:
csharp** **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.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System;
using System.Linq;

public class Test : MonoBehaviour
{
    private static readonly KeyCode[] KeyCodes = Enum.GetValues(typeof(KeyCode))
        .Cast<KeyCode>()
        .Where(k => ((int)k < (int)KeyCode.Mouse0))
        .ToArray();

    private void Update()
    {
        if (Input.anyKeyDown)

            for (int i = 0; i < KeyCodes.Length; i++)
            {
                KeyCode keycode = KeyCodes[i];
                if (Input.GetKeyDown(keycode))
                {
                    Debug.Log($"Input detected: {keycode.ToString()}");
                }
            }
    }
}

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: in 2021.2.0a16 - Ubuntu based Linux - Editor doesn't start maximized ... ), I cannot report the bug.

Thank you for helping about that @LeonhardP ! :slight_smile:

Same problem in 2021.1.0a5 and the bug reporter doesn’t work either.

Thanks @ ,

We’ll look into it.

1 Like

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

So, how is the investigation going? :slight_smile:

The problem is still there in 2021.1.0a9.

For info, @miniwolf_unity

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

The problem is still happening in 2021.1.0b1.

Just out of curiosity, does for example GetKeyDown(“1”) work even though KeyCode.Alpha1 doesn’t?

Hi @ ,

The issue with the bug reporter is currently being fixed and you can track the status of it here: Unity Issue Tracker - [Bug Reporter] [Linux] [2021.1] Bug Reporter fails to open when using Ubuntu 20.04

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.

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. :slight_smile:

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. :slight_smile:

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

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.

What @LeonhardP is saying is you want to use GetKey with the shift keys… i.e.:

if (Input.GetKeyDown(KeyCode.Alpha1))
{
  if (Input.GetKey(KeyCode.LeftShift) || Input.GetKey(KeyCode.RightShift))
  {
    Debug.Log("The alpha one (1) key was pressed.");
  }
}

…otherwise, you would have to press the Alpha1 key AND a shift key in exactly the same frame.

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. :slight_smile:

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. :slight_smile:

Looks like you’re using the AZERTY keyboard layout.

With that layout you need to hold shift to return numerals.

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.

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

using UnityEngine;
using System;
using System.Linq;

public class Test : MonoBehaviour
{
    private GameObject dirLight;

    // Start is called before the first frame update
    private void Start()
    {
        dirLight = GameObject.Find("Directional Light");
    }

    private static readonly KeyCode[] KeyCodes = Enum.GetValues(typeof(KeyCode))
        .Cast<KeyCode>()
        .Where(k => ((int)k < (int)KeyCode.Mouse0))
        .ToArray();

    // Update is called once per frame
    private void Update()
    {
        if (Input.anyKeyDown)

            for (int i = 0; i < KeyCodes.Length; i++)
            {
                KeyCode keycode = KeyCodes[i];
                if (Input.GetKeyDown(keycode))
                {
                    Debug.Log($"Input detected: {keycode.ToString()}");
                    dirLight.SetActive(false);
                }
            }
    }
}

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… :frowning:

And Happy New Year! :slight_smile:

1 Like

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,

Input detected: Alpha1
UnityEngine.Debug:Log (object)
Test:Update () (at Assets/Test.cs:32)

and

Alpha1 was pressed
UnityEngine.Debug:Log (object)
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.

1 Like

No need to apologize. Don’t let that discourage you from reporting/discussing your findings.

1 Like

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:

    private void Update()
    {
        if (Input.GetKeyDown(KeyCode.Alpha1))
            TriggerItemUse(101);
        if (Input.GetKeyDown(KeyCode.Alpha2))
            TriggerItemUse(102);
        if (Input.GetKeyDown(KeyCode.Alpha3))
            TriggerItemUse(103);
        if (Input.GetKeyDown(KeyCode.Alpha4))
            TriggerItemUse(104);
    }

Do you know anything about that?