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