- Home /
Key press event returns none key code
Here is a code for handling key pressing event:
Event keyEvent;
KeyCode newKey;
void OnGUI()
{
keyEvent = Event.current;
if (keyEvent.isKey)
{
newKey = keyEvent.keyCode;
Debug.Log (newKey.ToString ());
}
}
When I press the key which has character value, (i.e. a-z and 0-9) I get the next log output (for example I press 'A'):
A
UnityEngine.Debug:Log(Object)
None
UnityEngine.Debug:Log(Object)
So, why key press event returns none key code ? And how can I solve this issue ?
Answer by Bunny83 · Nov 07, 2017 at 01:17 AM
First if all "isKey" returns true for both, KeyDown and KeyUp events as it's literally defined like this:
public bool isKey
{
get
{
EventType type = this.type;
return type == EventType.KeyDown || type == EventType.KeyUp;
}
}
Next is that a key event does not necessarily have a KeyCode as you can read in the docs:
Note that key presses can come as separate events, one with valid Event.keyCode, and another with valid Event.character. In case of keyboard layouts with dead keys, multiple Event.keyCode events can generate a single Event.character event.
So your second event is most likely the "character" event.
I don't understand your problem... Why do you care about the character event if you only care about the keycode events? If the keycode is none, just ignore the event...
Also as i said "is$$anonymous$$ey" can be both, a $$anonymous$$eyDown or a $$anonymous$$eyUp event. In almost all cases you want to check one of the two and not treating both equal.
You haven't mentioned anything in your question about what this is actually good for? If you want to gather an input string you don't want to use $$anonymous$$eyCode but rather the "character". For pure $$anonymous$$eyCode events the character will be the null character '\0'.
We can't tell you how to fix something when we don't know whats the problem. Your question just states how the event system works without specifying a problem or a goal that can't be reached due to this behaviour.
I've implemented an input manager and I need to assign pressed key to a specific control setting. So, I need to handle any key which is pressed: 'A', 'B', 'Left shift', 'Right Alt' etc.
Answer by MaxGuernseyIII · Nov 07, 2017 at 09:30 PM
If you like feeling sad, you'll really get a kick out of this workaround.
using System;
using System.Collections.Generic;
using System.Linq;
using UnityEngine;
using UnityEngine.UI;
public class KeyCodeTest : MonoBehaviour
{
public Toggle LeftShiftDown;
public Queue<KeyCode> KeyDowns = new Queue<KeyCode>();
void Update()
{
MonitorKeys();
while (KeyDowns.Count > 0)
Debug.Log("Key down: " + KeyDowns.Dequeue());
}
void MonitorKeys()
{
foreach (var keyCode in Enum.GetValues(typeof(KeyCode)).Cast<KeyCode>().Where(Input.GetKeyDown))
KeyDowns.Enqueue(keyCode);
}
}
I suppose you could be a little smarter about it... you could make people subscribe to events that were indexed by key codes and only iterate through the codes with subscriptions.
Answer by GreenCubeGames · Feb 20, 2021 at 12:59 AM
private void OnGUI()
{
Event e = Event.current;
if (e.isKey)
{
if (e.keyCode != KeyCode.None)
{
if (e.type == EventType.KeyDown)
{
Debug.Log(e.keyCode + " Pressed");
}
if (e.type == EventType.KeyUp)
{
Debug.Log(e.keyCode + " Up");
}
}
}
}