- Home /
How to detect if any key has been pressed down
I dont want to put detection for each key so can i detect if ANY key has been pressed down?
Answer by jashan · Jul 27, 2010 at 10:27 AM
You could check for keyDown events in OnGUI: EventType, Event, EventType.KeyDown, event.type
So, I guess the code would look something like this (not tested):
public void OnGUI() { if (Event.current.type == EventType.KeyDown) { KeyPressedEventHandler(); } }
private void KeyPressedEventHandler() { // do whatever you want to do when a key was pressed ;-) }
UnityScript / JavaScript would look almost the same - just replace "public void" with "function" and "private void" with "function" and it should compile in JavaScript/UnityScript ;-)
is the keypressedeventhandler a preset function or you just named it that way?
Well it worked well , my achievement system thanks you :D
I just named it that way. Actually, you don't even have to put that into an own method but usually, that'll make your code more readable.
Answer by sovalopivia · Jul 27, 2010 at 12:36 PM
You can also use Input.anyKeyDown()
void Update()
{
if (Input.anyKeyDown())
{
DoSomething()
}
}
I was about to say the exact same, +1
Since it's Input you are doing, check the Input class in the scripting documentation Unity 3D has placed on it's website. That should be made common practice for anyone that ever looks at code. Just look up the class docs
Input.Any$$anonymous$$eyDown() was not present when i had asked the question.
Answer by AcroYogi · Oct 05, 2011 at 04:09 PM
NOTE: In JavaScript, the proper syntax is:
function Update () {
if (Input.anyKeyDown) {
DoSomething();
}
}