Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Jquery detect if keyboard key is held down

I'm making a crossword application using PhoneGap, and need to detect if the user has just done a quick press on the keyboard or has held down a key to get a special character. What would be the event to detect this?

EDIT: When I press the button and don't release it, then keyup is triggered before I release the key. IPhone doesn't have this problem, and the emulator with SDK 8 version doesn't have this issue too. The HTC Desire S and possibly other HTC phones may have this 'feature'. Looks lika a bug to me.

like image 781
Demonick Avatar asked Oct 23 '22 05:10

Demonick


1 Answers

try this if you want to know at any moment which keys are pressed:

var keys = array();
$(windows).keydown(function(e){
    keys[e.which] = true;
}
$(windows).keyup(function(e){
    keys[e.which] = false;
}

If you just want to trigger an action when a user press a key for a while, just set a timeout which you can cancel if keyup is triggered before the desired time.

like image 92
Alytrem Avatar answered Oct 27 '22 10:10

Alytrem