Well I searched on Google but still didn’t found the answer I was looking for.
I want to check if the user pressed a key, something like this –
if(document.onkeyup) {
// Some Stuff here
}
I know I can do this, this way –
document.onkeyup = getKey;
But the function getKey cannot return values.
So how can I check if the user pressed a key?
EDIT : I need pure Javascript for this thing..
You can do this in pure Javascript using the event object, without the need of external libraries such as jQuery.
To capture the keycode, just pass the event as parameter of getKey function:
Frequently used keyCode list:
For a usefull list of keyCodes, you can check out this URL:
http://www.cambiaresearch.com/articles/15/javascript-char-codes-key-codes
Setting the keyCode to a global variable:
If you are interested in capturing the keyCode for later usage, you can do something like this:
Setting the keyCode to the event source object:
If you don’t like global variables, like me, you could also do something like this: