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 ?

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.

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

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");
}
}
}
}