11 April 2012
See jQuery keyboard events for yourself
return false on different events:
- evt.type — keydown, keyup, or keypress
- evt.which — jQuery’s normalized value for keyCode & charCode
- any special keys that are pressed—evt.altKey, evt.metaKey, or evt.shiftKey
Keypress and key repeats
Keypress is a natural go-to for registering when keys repeat. However, it’s worth testing things in different browsers, e.g., firefox triggers keypress events for arrow keys while webkit does not.
safekeypress.js is a jQuery plugin I wrote to get reliable key repeats at computers natural repeat rate.
Keydown will repeat for regular keys, which you may not want in some situations. Maybe you want something like a piano to trigger on keydown, but not start repeating, or you want to manually control the rate at which repeats are triggered.
PressedKeys.js is a simple script that lets you just check an object to see if a key is down or not.