2014-01-30 13:40:36 +00:00
|
|
|
/*\
|
|
|
|
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
|
2014-02-04 20:05:34 +00:00
|
|
|
ctrl+shift+alt+A
|
2014-01-30 13:40:36 +00:00
|
|
|
*/
|
|
|
|
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;
|
2014-09-02 09:41:48 +00:00
|
|
|
} else if(s === "meta") {
|
|
|
|
info.metaKey = true;
|
2014-01-30 13:40:36 +00:00
|
|
|
}
|
|
|
|
// Replace named keys with their code
|
|
|
|
if(namedKeys[s]) {
|
|
|
|
info.keyCode = namedKeys[s];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return info;
|
|
|
|
};
|
|
|
|
|
2014-09-02 09:41:48 +00:00
|
|
|
exports.checkKeyDescriptor = function(event,keyInfo) {
|
2014-09-02 21:09:28 +00:00
|
|
|
var metaKeyStatus = !!keyInfo.metaKey; // Using a temporary variable to keep JSHint happy
|
2014-09-02 09:41:48 +00:00
|
|
|
return event.keyCode === keyInfo.keyCode &&
|
|
|
|
event.shiftKey === keyInfo.shiftKey &&
|
|
|
|
event.altKey === keyInfo.altKey &&
|
|
|
|
event.ctrlKey === keyInfo.ctrlKey &&
|
2014-09-02 21:09:28 +00:00
|
|
|
event.metaKey === metaKeyStatus;
|
2014-09-02 09:41:48 +00:00
|
|
|
};
|
|
|
|
|
2014-01-30 13:40:36 +00:00
|
|
|
})();
|