The primary way to use the library is by calling the hotkeys function directly. You can define single keys, key combinations, or multiple shortcuts separated by commas.
To prevent default browser behavior (like page refresh), call event.preventDefault() inside the handler. To stop the event and prevent default browser events entirely, return false from the handler.
Supported Modifiers:
⇧, shiftoption, ⌥, altctrl, controlcommand, ⌘
Special Keys:
backspace, tab, clear, enter, return, esc, escape, space, up, down, left, right, home, end, pageup, pagedown, del, delete, f1 through f19, num_0 through num_9, num_multiply, num_add, num_enter, num_subtract, num_decimal, num_divide.
// Single key
hotkeys('a', function(event, handler){
alert('you pressed a!')
});
// Key Combination
hotkeys('ctrl+a,ctrl+b,r,f', function (event, handler){
switch (handler.key) {
case 'ctrl+a': alert('you pressed ctrl+a!');
break;
// ...
}
});
// Preventing default behavior
hotkeys('f5', function(event, handler) {
event.preventDefault();
alert('you pressed F5!');
});
// Stopping event propagation and default behavior
hotkeys('ctrl+r, command+r', function() {
alert('stopped reload!');
return false;
});