2018-08-27 12:30:50 +00:00
|
|
|
import keysyms from "./keysymdef.js";
|
|
|
|
import vkeys from "./vkeys.js";
|
|
|
|
import fixedkeys from "./fixedkeys.js";
|
|
|
|
import DOMKeyTable from "./domkeytable.js";
|
|
|
|
import * as browser from "../util/browser.js";
|
2018-08-14 12:11:49 +00:00
|
|
|
|
|
|
|
// Get 'KeyboardEvent.code', handling legacy browsers
|
2019-06-24 11:26:25 +00:00
|
|
|
export function getKeycode(evt) {
|
2018-08-14 12:11:49 +00:00
|
|
|
// Are we getting proper key identifiers?
|
|
|
|
// (unfortunately Firefox and Chrome are crappy here and gives
|
|
|
|
// us an empty string on some platforms, rather than leaving it
|
|
|
|
// undefined)
|
|
|
|
if (evt.code) {
|
|
|
|
// Mozilla isn't fully in sync with the spec yet
|
|
|
|
switch (evt.code) {
|
2018-08-27 12:30:50 +00:00
|
|
|
case 'OSLeft': return 'MetaLeft';
|
|
|
|
case 'OSRight': return 'MetaRight';
|
2018-08-14 12:11:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return evt.code;
|
|
|
|
}
|
|
|
|
|
|
|
|
// The de-facto standard is to use Windows Virtual-Key codes
|
|
|
|
// in the 'keyCode' field for non-printable characters. However
|
|
|
|
// Webkit sets it to the same as charCode in 'keypress' events.
|
2018-08-27 12:30:50 +00:00
|
|
|
if ((evt.type !== 'keypress') && (evt.keyCode in vkeys)) {
|
2019-06-24 11:26:25 +00:00
|
|
|
let code = vkeys[evt.keyCode];
|
2018-08-14 12:11:49 +00:00
|
|
|
|
|
|
|
// macOS has messed up this code for some reason
|
2018-08-27 12:30:50 +00:00
|
|
|
if (browser.isMac() && (code === 'ContextMenu')) {
|
2018-08-14 12:11:49 +00:00
|
|
|
code = 'MetaRight';
|
|
|
|
}
|
|
|
|
|
|
|
|
// The keyCode doesn't distinguish between left and right
|
|
|
|
// for the standard modifiers
|
|
|
|
if (evt.location === 2) {
|
|
|
|
switch (code) {
|
2018-08-27 12:30:50 +00:00
|
|
|
case 'ShiftLeft': return 'ShiftRight';
|
|
|
|
case 'ControlLeft': return 'ControlRight';
|
|
|
|
case 'AltLeft': return 'AltRight';
|
2018-08-14 12:11:49 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Nor a bunch of the numpad keys
|
|
|
|
if (evt.location === 3) {
|
|
|
|
switch (code) {
|
2018-08-27 12:30:50 +00:00
|
|
|
case 'Delete': return 'NumpadDecimal';
|
|
|
|
case 'Insert': return 'Numpad0';
|
|
|
|
case 'End': return 'Numpad1';
|
|
|
|
case 'ArrowDown': return 'Numpad2';
|
|
|
|
case 'PageDown': return 'Numpad3';
|
|
|
|
case 'ArrowLeft': return 'Numpad4';
|
|
|
|
case 'ArrowRight': return 'Numpad6';
|
|
|
|
case 'Home': return 'Numpad7';
|
|
|
|
case 'ArrowUp': return 'Numpad8';
|
|
|
|
case 'PageUp': return 'Numpad9';
|
|
|
|
case 'Enter': return 'NumpadEnter';
|
2018-08-14 12:11:49 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return code;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 'Unidentified';
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get 'KeyboardEvent.key', handling legacy browsers
|
2018-08-27 12:30:50 +00:00
|
|
|
export function getKey(evt) {
|
2018-08-14 12:11:49 +00:00
|
|
|
// Are we getting a proper key value?
|
|
|
|
if (evt.key !== undefined) {
|
|
|
|
// IE and Edge use some ancient version of the spec
|
|
|
|
// https://developer.microsoft.com/en-us/microsoft-edge/platform/issues/8860571/
|
|
|
|
switch (evt.key) {
|
2018-08-27 12:30:50 +00:00
|
|
|
case 'Spacebar': return ' ';
|
|
|
|
case 'Esc': return 'Escape';
|
|
|
|
case 'Scroll': return 'ScrollLock';
|
|
|
|
case 'Win': return 'Meta';
|
|
|
|
case 'Apps': return 'ContextMenu';
|
|
|
|
case 'Up': return 'ArrowUp';
|
|
|
|
case 'Left': return 'ArrowLeft';
|
|
|
|
case 'Right': return 'ArrowRight';
|
|
|
|
case 'Down': return 'ArrowDown';
|
|
|
|
case 'Del': return 'Delete';
|
|
|
|
case 'Divide': return '/';
|
|
|
|
case 'Multiply': return '*';
|
|
|
|
case 'Subtract': return '-';
|
|
|
|
case 'Add': return '+';
|
|
|
|
case 'Decimal': return evt.char;
|
2018-08-14 12:11:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Mozilla isn't fully in sync with the spec yet
|
|
|
|
switch (evt.key) {
|
2018-08-27 12:30:50 +00:00
|
|
|
case 'OS': return 'Meta';
|
2018-08-14 12:11:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// iOS leaks some OS names
|
|
|
|
switch (evt.key) {
|
2018-08-27 12:30:50 +00:00
|
|
|
case 'UIKeyInputUpArrow': return 'ArrowUp';
|
|
|
|
case 'UIKeyInputDownArrow': return 'ArrowDown';
|
|
|
|
case 'UIKeyInputLeftArrow': return 'ArrowLeft';
|
|
|
|
case 'UIKeyInputRightArrow': return 'ArrowRight';
|
|
|
|
case 'UIKeyInputEscape': return 'Escape';
|
2018-08-14 12:11:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// IE and Edge have broken handling of AltGraph so we cannot
|
|
|
|
// trust them for printable characters
|
2018-08-27 12:30:50 +00:00
|
|
|
if ((evt.key.length !== 1) || (!browser.isIE() && !browser.isEdge())) {
|
2018-08-14 12:11:49 +00:00
|
|
|
return evt.key;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Try to deduce it based on the physical key
|
2019-06-24 11:26:25 +00:00
|
|
|
const code = getKeycode(evt);
|
2018-08-27 12:30:50 +00:00
|
|
|
if (code in fixedkeys) {
|
|
|
|
return fixedkeys[code];
|
2018-08-14 12:11:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// If that failed, then see if we have a printable character
|
|
|
|
if (evt.charCode) {
|
|
|
|
return String.fromCharCode(evt.charCode);
|
|
|
|
}
|
|
|
|
|
|
|
|
// At this point we have nothing left to go on
|
|
|
|
return 'Unidentified';
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get the most reliable keysym value we can get from a key event
|
2019-06-24 11:26:25 +00:00
|
|
|
export function getKeysym(evt) {
|
|
|
|
const key = getKey(evt);
|
2018-08-14 12:11:49 +00:00
|
|
|
|
|
|
|
if (key === 'Unidentified') {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
// First look up special keys
|
2018-08-27 12:30:50 +00:00
|
|
|
if (key in DOMKeyTable) {
|
2019-06-24 11:26:25 +00:00
|
|
|
let location = evt.location;
|
2018-08-14 12:11:49 +00:00
|
|
|
|
|
|
|
// Safari screws up location for the right cmd key
|
2018-08-27 12:30:50 +00:00
|
|
|
if ((key === 'Meta') && (location === 0)) {
|
2018-08-14 12:11:49 +00:00
|
|
|
location = 2;
|
|
|
|
}
|
|
|
|
|
2018-08-27 12:30:50 +00:00
|
|
|
if ((location === undefined) || (location > 3)) {
|
2018-08-14 12:11:49 +00:00
|
|
|
location = 0;
|
|
|
|
}
|
|
|
|
|
2018-08-27 12:30:50 +00:00
|
|
|
return DOMKeyTable[key][location];
|
2018-08-14 12:11:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Now we need to look at the Unicode symbol instead
|
|
|
|
|
|
|
|
// Special key? (FIXME: Should have been caught earlier)
|
|
|
|
if (key.length !== 1) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2019-06-24 11:26:25 +00:00
|
|
|
const codepoint = key.charCodeAt();
|
2018-08-14 12:11:49 +00:00
|
|
|
if (codepoint) {
|
2018-08-27 12:30:50 +00:00
|
|
|
return keysyms.lookup(codepoint);
|
2018-08-14 12:11:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return null;
|
2018-08-27 12:30:50 +00:00
|
|
|
}
|