Keybinding documentation
Allows you to bind and unbind events to individual keypresses or sequences of keypresses. Intended for implementing keyboard shortcuts.
Allows you to bind and unbind events to individual keypresses or sequences of keypresses. Intended for implementing keyboard shortcuts.
bind(keyString, fn, options)
Binds a callback to a specified keypress.
A string of the key or key combination or sequence to bind to. For a list of key values, see Key Values on MDN.
For each key binding, the user's keypresses will be recorded up to the length of the bound sequence. Once the event is fired, the logged keypresses are deleted.
Keypresses will never be recorded while keyboard focus is in a password input, and by default they will also not be recorded while keyboard focus is in any element that expects keyboard input.
Some key names also have aliases:
'space'
'spacebar'
'up'
'right'
'down'
'left'
'esc'
Modifier keys cannot be detected directly, but can be combined with other keys using '+'
, e.g. 'ctrl+m'
'alt'
'control'
'ctrl'
'command'
'cmd'
'meta'
'shift'
Sequences of keys can be bound using keyStrings like 'ctrl+k ctrl+b'
The function to bind to the keypress, using a keydown event.
An options object with any of these properties:
boolean
false
unbind(keyString, fn)
Unbinds a function from a specified key or set of keys.
A string of the key or keys the event is bound to.
The function to unbind to the keypress.
Try the following keyboard shortcuts/sequences:
Keyboard shortcuts won't fire when your focus is in an input, unless it's specified that it should.
Keep in mind that only some browser shortcuts (e.g. Ctrl + s) can have their default action prevented with event.preventDefault()
. If you ever do this, be sure to test it in multiple browsers.