mirror of
https://github.com/Jermolene/TiddlyWiki5
synced 2025-12-10 02:38:06 +00:00
Add experimental keyboard shortcut widget
It’s not cool that we have to use a separate keyboard widget for each keyboard shortcut. Fixes #386
This commit is contained in:
60
core/modules/utils/dom/keyboard.js
Normal file
60
core/modules/utils/dom/keyboard.js
Normal file
@@ -0,0 +1,60 @@
|
||||
/*\
|
||||
title: $:/core/modules/utils/dom/keyboard.js
|
||||
type: application/javascript
|
||||
module-type: utils
|
||||
|
||||
Keyboard utilities
|
||||
|
||||
\*/
|
||||
(function(){
|
||||
|
||||
/*jslint node: true, browser: true */
|
||||
/*global $tw: false */
|
||||
"use strict";
|
||||
|
||||
var namedKeys = {
|
||||
"backspace": 8,
|
||||
"tab": 9,
|
||||
"enter": 13,
|
||||
"escape": 27
|
||||
};
|
||||
|
||||
/*
|
||||
Parses a key descriptor into the structure:
|
||||
{
|
||||
keyCode: numeric keycode
|
||||
shiftKey: boolean
|
||||
altKey: boolean
|
||||
ctrlKey: boolean
|
||||
}
|
||||
Key descriptors have the following format:
|
||||
ctrl+enter
|
||||
ctrl+A
|
||||
*/
|
||||
exports.parseKeyDescriptor = function(keyDescriptor) {
|
||||
var components = keyDescriptor.split("+"),
|
||||
info = {
|
||||
keyCode: 0,
|
||||
shiftKey: false,
|
||||
altKey: false,
|
||||
ctrlKey: false
|
||||
};
|
||||
for(var t=0; t<components.length; t++) {
|
||||
var s = components[t].toLowerCase();
|
||||
// Look for modifier keys
|
||||
if(s === "ctrl") {
|
||||
info.ctrlKey = true;
|
||||
} else if(s === "shift") {
|
||||
info.shiftKey = true;
|
||||
} else if(s === "alt") {
|
||||
info.altKey = true;
|
||||
}
|
||||
// Replace named keys with their code
|
||||
if(namedKeys[s]) {
|
||||
info.keyCode = namedKeys[s];
|
||||
}
|
||||
}
|
||||
return info;
|
||||
};
|
||||
|
||||
})();
|
||||
Reference in New Issue
Block a user