]>
git.proxmox.com Git - mirror_novnc.git/blob - core/input/util.js
1 import KeyTable
from "./keysym.js";
2 import keysyms
from "./keysymdef.js";
3 import vkeys
from "./vkeys.js";
4 import fixedkeys
from "./fixedkeys.js";
5 import DOMKeyTable
from "./domkeytable.js";
6 import * as browser
from "../util/browser.js";
8 // Get 'KeyboardEvent.code', handling legacy browsers
9 export function getKeycode(evt
){
10 // Are we getting proper key identifiers?
11 // (unfortunately Firefox and Chrome are crappy here and gives
12 // us an empty string on some platforms, rather than leaving it
15 // Mozilla isn't fully in sync with the spec yet
17 case 'OSLeft': return 'MetaLeft';
18 case 'OSRight': return 'MetaRight';
24 // The de-facto standard is to use Windows Virtual-Key codes
25 // in the 'keyCode' field for non-printable characters. However
26 // Webkit sets it to the same as charCode in 'keypress' events.
27 if ((evt
.type
!== 'keypress') && (evt
.keyCode
in vkeys
)) {
28 var code
= vkeys
[evt
.keyCode
];
30 // macOS has messed up this code for some reason
31 if (browser
.isMac() && (code
=== 'ContextMenu')) {
35 // The keyCode doesn't distinguish between left and right
36 // for the standard modifiers
37 if (evt
.location
=== 2) {
39 case 'ShiftLeft': return 'ShiftRight';
40 case 'ControlLeft': return 'ControlRight';
41 case 'AltLeft': return 'AltRight';
45 // Nor a bunch of the numpad keys
46 if (evt
.location
=== 3) {
48 case 'Delete': return 'NumpadDecimal';
49 case 'Insert': return 'Numpad0';
50 case 'End': return 'Numpad1';
51 case 'ArrowDown': return 'Numpad2';
52 case 'PageDown': return 'Numpad3';
53 case 'ArrowLeft': return 'Numpad4';
54 case 'ArrowRight': return 'Numpad6';
55 case 'Home': return 'Numpad7';
56 case 'ArrowUp': return 'Numpad8';
57 case 'PageUp': return 'Numpad9';
58 case 'Enter': return 'NumpadEnter';
65 return 'Unidentified';
68 // Get 'KeyboardEvent.key', handling legacy browsers
69 export function getKey(evt
) {
70 // Are we getting a proper key value?
71 if (evt
.key
!== undefined) {
72 // IE and Edge use some ancient version of the spec
73 // https://developer.microsoft.com/en-us/microsoft-edge/platform/issues/8860571/
75 case 'Spacebar': return ' ';
76 case 'Esc': return 'Escape';
77 case 'Scroll': return 'ScrollLock';
78 case 'Win': return 'Meta';
79 case 'Apps': return 'ContextMenu';
80 case 'Up': return 'ArrowUp';
81 case 'Left': return 'ArrowLeft';
82 case 'Right': return 'ArrowRight';
83 case 'Down': return 'ArrowDown';
84 case 'Del': return 'Delete';
85 case 'Divide': return '/';
86 case 'Multiply': return '*';
87 case 'Subtract': return '-';
88 case 'Add': return '+';
89 case 'Decimal': return evt
.char;
92 // Mozilla isn't fully in sync with the spec yet
94 case 'OS': return 'Meta';
97 // iOS leaks some OS names
99 case 'UIKeyInputUpArrow': return 'ArrowUp';
100 case 'UIKeyInputDownArrow': return 'ArrowDown';
101 case 'UIKeyInputLeftArrow': return 'ArrowLeft';
102 case 'UIKeyInputRightArrow': return 'ArrowRight';
103 case 'UIKeyInputEscape': return 'Escape';
106 // IE and Edge have broken handling of AltGraph so we cannot
107 // trust them for printable characters
108 if ((evt
.key
.length
!== 1) || (!browser
.isIE() && !browser
.isEdge())) {
113 // Try to deduce it based on the physical key
114 var code
= getKeycode(evt
);
115 if (code
in fixedkeys
) {
116 return fixedkeys
[code
];
119 // If that failed, then see if we have a printable character
121 return String
.fromCharCode(evt
.charCode
);
124 // At this point we have nothing left to go on
125 return 'Unidentified';
128 // Get the most reliable keysym value we can get from a key event
129 export function getKeysym(evt
){
130 var key
= getKey(evt
);
132 if (key
=== 'Unidentified') {
136 // First look up special keys
137 if (key
in DOMKeyTable
) {
138 var location
= evt
.location
;
140 // Safari screws up location for the right cmd key
141 if ((key
=== 'Meta') && (location
=== 0)) {
145 if ((location
=== undefined) || (location
> 3)) {
149 return DOMKeyTable
[key
][location
];
152 // Now we need to look at the Unicode symbol instead
156 // Special key? (FIXME: Should have been caught earlier)
157 if (key
.length
!== 1) {
161 codepoint
= key
.charCodeAt();
163 return keysyms
.lookup(codepoint
);