HotKey provides functionality similar to the accesskey attribute, but has many enhancements that allow for more granular control when creating keyboard driven interfaces.
Active HotKeys on This Page
Letter
Options
control + a
{}
control + shift + b
{shiftKey: true}
control + shift + alt/option + c
{altKey: true, shiftKey: true}
d - no modifiers
{ctrlKey: false}
control + e (if second textarea below has focus)
{element: $('containment_textarea')}
control + down (if second textarea below has focus)
You can call the trigger() method on any HotKey instance to execute the callback. The callback will not receive an Event object in this case, as it would if it had been triggered by the keyboard.
HotKey Element Binding
It is possible to apply the HotKey to a given element by passing the element option. If you bind all HotKeys to elements it is possible to register multiple keys with the same modifiers to different element.
Example
new HotKey('a',function(event){
log('control + "a" pressed');
});
new HotKey('b',function(event){
log('control + shift + "b" pressed');
},{
shiftKey: true
});
new HotKey('c',function(event){
log('control + shift + alt/option + "c" pressed');
},{
shiftKey: true,
altKey: true
});
new HotKey('d',function(event){
log('"d" pressed');
},{
ctrlKey: false
});
new HotKey('e',function(event){
log('control + "e" pressed inside of textarea');
},{
element: $('containment_textarea')
});
//you can also register against any of the special keys
//defined in prototype.js
//up, down, left, right, esc, return, tab, etc
new HotKey('down',function(event){
log('control + "down" pressed inside of textarea');
},{
element: $('containment_textarea')
});
//remote trigger
$('remote_trigger').observe('click',function(event){
var value = $('remote_trigger_selector').value;
HotKey.hotkeys.find(function(hotkey){
//hotkey.letter will always be upper case
return hotkey.letter == value;
}).trigger();
event.stop();
});
Class
Return
Name
Description
array
hotkeys
Instance
Return
Name
Description
HotKey
initialize(string letter, function callback [,Hash options])
null
destroy()
Also calls disable()
null
disable()
null
enable()
null
trigger()
Triggers callback, callback will not be passed an Event object.
Element
element
Defaults to document.body
string
letter
Always in upper case.
Options
Type
Name
Default
Description
bool
altKey
false
bool
ctrlKey
true
Defaults to true, emulating accesskey behavior.
Element || false
element
false
Attaches the hotkey to a given element instead of document.body
bool
shiftKey
false
Events
Name
Description
afterCallback()
beforeCallback()
Throwing $break will prevent the callback from being called.