- Home /
How can I detect Keyboard Events?
I recently copy/pasted some TileManager from here: http://wiki.unity3d.com/index.php?title=TileManager
However, it doesn't work. I started outputting Debug Logs to find out why it wasn't working and I discovered that keyboard events are never detected.
How can I get Unity to detect keyboard events?
Here are a few log statements that I used:
//get the current event
Event e = Event.current;
/////////////////////////////////////////////////////////////////////////////////
// The following if statements work correctly
if(e.shift)
{
Debug.Log("Shift");
}
if(e.type.Equals(EventType.MouseMove))
{
Debug.Log("Mouse Move");
}
/////////////////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////////////
// The following if statements do not execute even when they should
if(e.isKey) // Does not work
{
Debug.Log("Key");
}
if (e.Equals (Event.KeyboardEvent ("[enter]"))) // Does not work
{
Debug.Log("Enter");
}
if(e.type.Equals(EventType.KeyDown)) // Does not work
{
Debug.Log("Key Down");
}
/////////////////////////////////////////////////////////////////////////////////
do you need to use keyboard events? why not just use Input.Get$$anonymous$$eyDown($$anonymous$$eycode.A) ?
Answer by zombience · Aug 15, 2013 at 09:09 PM
I just tried using keyboard events in a custom editor.
this worked for me:
[CustomEditor(typeof(Manipulator))]
public class ManipulatorEditor : Editor
{
public override void OnInspectorGUI()
{
if (Event.current.keyCode == KeyCode.S)
{
Debug.Log("hit s");
}
}
}
note that this only works when you have the referenced class on an object (Manipulator.cs in this case), and have the inspector window of that object selected. the rest of the time, the keypresses will not be registered.
I just spent 2 or 3 hours trying to figure this out. The key part of this solution is that "have the inspector window of that object selected. the rest of the time, the keypresses will not be registered". ie, I was trying to press the keys in the hierarchy. So frustrating!
Is there a way to do something similar in the hierarchy?
Answer by Ali-Nagori · May 30, 2014 at 03:59 PM
if you want these Event in your case place your code inside OnGUI
void OnGUI() { ........... ...........
Answer by highpockets · Aug 14, 2013 at 04:52 PM
Input.GetKeyDown("space"); for example, if you want the space bar to do something
Find more input functions here: http://docs.unity3d.com/Documentation/ScriptReference/Input.html
Answer by Bonobo · Aug 15, 2013 at 03:13 PM
I have tried using Input instead of Events.
The problem with this solution is that this script is for an Editor extension that runs even when the game isn't playing.
Input only seems to work when I actually run the game itself.
you've added a new "answer" to your own question. please add comments ins$$anonymous$$d.
Your answer
Follow this Question
Related Questions
Event Trigger For Key Pressed 4 Answers
EventSystem lag on first touch 0 Answers
checking for keyboard input with exceptions 0 Answers