- Home /
Why is this script showing all keycodes except shift?
as the header says...here the script, it shows all except shift keys, why?!
using UnityEngine;
using System.Collections;
public class RainInput : MonoBehaviour
{
public string event1;
void Update()
{
if (event1.Length > 0)
{
Debug.Log(event1);
}
event1 = "";
}
void OnGUI()
{
Event e = Event.current;
{
string tmpstr = "" + e.keyCode;
if (tmpstr == "None")
{
return;
}
event1 = tmpstr;
}
}
}
Answer by Bunny83 · May 14, 2012 at 09:32 PM
Because the Event class is ment for GUI key input quite similar to the windows char-messages. Shift, Ctrl and Alt are modifier keys so usually you don't execute an action when pressing those keys. They change the behaviour of other keys.
To sum up:
Event class is used for GUI input
Input class is used to process any game input(including shift, alt, ...).
Event class can only be used in OnGUI
Input class should only be used in Update or LateUpdate
edit
When using the event class you should always check the event type. Here you can see all possible events. Unity processes a lot events with OnGUI. It is called for each event. Usually 2 times per frame (Layout and Repaint event), but also for any kind of key or mouse event (the mouse move event only exists in the editor afaik).
void OnGUI()
{
Event e = Event.current;
if (e.type == EventType.KeyDown)
{
if (e.keyCode == KeyCode.A)
// ...
}
}
void Update()
{
if (Input.GetKeyDown(KeyCode.LeftShift ))
{
Debug.Log("LeftShift has been pressed");
}
}
Here's the list of all key codes
thanks i use now Input.Get$$anonymous$$eyDown i was just wondering, it really shows even left control and rightcontrol but not shift if you use Event... anyway best solution is Input.Get$$anonymous$$eyDown atm i think.
But keep in $$anonymous$$d that you shouldn't use the Input class in OnGUI, since OnGUI is called to process events and it can get called multiple times per frame.
Answer by Bouldeterre · Feb 06, 2013 at 03:20 AM
Look at http://docs.unity3d.com/Documentation/ScriptReference/Event-shift.html http://docs.unity3d.com/Documentation/ScriptReference/Event-control.html http://docs.unity3d.com/Documentation/ScriptReference/Event-alt.html
Shift, Ctrl and Alt can be checked as event with this.