Web Component: j-Shortcuts
j-Shortcuts
This component can help with creating keyboard shortcuts. The component is singleton.
Methods:
component.register(shortcut, callback(e), [prevent], [preparation(e)])- registers a new shortcutcomponent.exec('shortcut')- evaluates shortcut
Shortcuts:
enteresctabcmd,meta,winshift,capslockctrlaltspacebackspace,del,deleteleft,up,right,downF1,F2,F3,F4,F5,F6,F7,F8,F9,F10,F11,F12- or custom key-codes
{Number}or key-characters{String(1)} insremovemeansbackspaceordeletesaveselectallcloneundoredo- supports
.shortcut, read more below
Example:
// SETTER(true, ...) --> works in jComponent +v9.0.0
SETTER(true, 'shortcuts/register', 'cmd + enter', function(e) {
console.log('PRESSED: CMD + ENTER');
});
SETTER(true, 'shortcuts/register', 'alt + p', function(e) {
console.log('PRESSED: ALT + P');
});
SETTER(true, 'shortcuts/register', 'esc', function(e) {
console.log('PRESSED: esc');
});
// +v3 supports multiple shortcuts at the one time:
SETTER(true, 'shortcuts/register', 'cmd+enter, ctrl+enter', function(e) {
console.log('PRESSED: CMD + ENTER');
});
// NEW: you can make preparation for the shortcut
SETTER(true, 'shortcuts/register', 'cmd+enter, ctrl+enter', function(e) {
console.log('PRESSED: CMD + ENTER');
}, function(e) {
// You can enable/disable shortcut manually in the "preparation function"
// but you need to "return {Boolean}", true will approve shortcut (it calls of the shortcut "callback")
// otherwise "returning false" the shorcut will be skipped
return e.target.tagName !== 'INPUT';
});
New inline declaration:
<button class="exec shortcut" data-shortcut="F1" data-exec="METHO_TO_CALL">Help</button>
.shortcutclass will emitclickevent- property
e.shortcutwill contain Event instance from thekeydownevent
New session shortcuts:
// Open session for new shortcuts
// The methods disables all registered shortcuts while session is not end
SETTER('shortcuts/session', function(register) {
register('esc', function() {
// Cancels session
SETTER('shortcuts/end');
});
});
Author
- Peter Širka petersirka@gmail.com
- License
Created
24. february 2017
Updated
01. february 2023
Version
v1
License
https://www.totaljs.com/license/
Responsive
Yes
Author
Peter Širka
Email
petersirka@gmail.com

