x


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?

more ▼

asked Jul 27 '10 at 10:18 AM

yeoldesnake 1 gravatar image

yeoldesnake 1
549 15 18 22

(comments are locked)
10|3000 characters needed characters left

3 answers: sort voted first

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

more ▼

answered Jul 27 '10 at 10:27 AM

jashan gravatar image

jashan
10.1k 25 40 116

is the keypressedeventhandler a preset function or you just named it that way?

Jul 27 '10 at 10:34 AM yeoldesnake 1

Well it worked well , my achievement system thanks you :D

Jul 27 '10 at 11:28 AM yeoldesnake 1

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.

Jul 28 '10 at 06:34 AM jashan
(comments are locked)
10|3000 characters needed characters left

You can also use Input.anyKeyDown()

void Update()
{
    if (Input.anyKeyDown())
    {
        DoSomething()
    }
}
more ▼

answered Jul 27 '10 at 12:36 PM

sovalopivia gravatar image

sovalopivia
393 2 6 15

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

Oct 29 '10 at 09:18 AM Proclyon

Input.AnyKeyDown() was not present when i had asked the question.

Aug 19 '11 at 02:20 PM yeoldesnake 1
(comments are locked)
10|3000 characters needed characters left

NOTE: In JavaScript, the proper syntax is:

function Update () {
    if (Input.anyKeyDown) {
        DoSomething();
    }
}
more ▼

answered Oct 05 '11 at 04:09 PM

AcroYogi gravatar image

AcroYogi
26 2 3 5

(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x172
x166

asked: Jul 27 '10 at 10:18 AM

Seen: 11924 times

Last Updated: Jul 24 '12 at 01:30 AM