]>
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
26 if (evt
.keyCode
in vkeys
) {
27 let code
= vkeys
[evt
.keyCode
];
29 // macOS has messed up this code for some reason
30 if (browser
.isMac() && (code
=== 'ContextMenu')) {
34 // The keyCode doesn't distinguish between left and right
35 // for the standard modifiers
36 if (evt
.location
=== 2) {
38 case 'ShiftLeft': return 'ShiftRight';
39 case 'ControlLeft': return 'ControlRight';
40 case 'AltLeft': return 'AltRight';
44 // Nor a bunch of the numpad keys
45 if (evt
.location
=== 3) {
47 case 'Delete': return 'NumpadDecimal';
48 case 'Insert': return 'Numpad0';
49 case 'End': return 'Numpad1';
50 case 'ArrowDown': return 'Numpad2';
51 case 'PageDown': return 'Numpad3';
52 case 'ArrowLeft': return 'Numpad4';
53 case 'ArrowRight': return 'Numpad6';
54 case 'Home': return 'Numpad7';
55 case 'ArrowUp': return 'Numpad8';
56 case 'PageUp': return 'Numpad9';
57 case 'Enter': return 'NumpadEnter';
64 return 'Unidentified';
67 // Get 'KeyboardEvent.key', handling legacy browsers
68 export function getKey(evt
) {
69 // Are we getting a proper key value?
70 if (evt
.key
!== undefined) {
71 // IE and Edge use some ancient version of the spec
72 // https://developer.microsoft.com/en-us/microsoft-edge/platform/issues/8860571/
74 case 'Spacebar': return ' ';
75 case 'Esc': return 'Escape';
76 case 'Scroll': return 'ScrollLock';
77 case 'Win': return 'Meta';
78 case 'Apps': return 'ContextMenu';
79 case 'Up': return 'ArrowUp';
80 case 'Left': return 'ArrowLeft';
81 case 'Right': return 'ArrowRight';
82 case 'Down': return 'ArrowDown';
83 case 'Del': return 'Delete';
84 case 'Divide': return '/';
85 case 'Multiply': return '*';
86 case 'Subtract': return '-';
87 case 'Add': return '+';
88 case 'Decimal': return evt
.char;
91 // Mozilla isn't fully in sync with the spec yet
93 case 'OS': return 'Meta';
94 case 'LaunchMyComputer': return 'LaunchApplication1';
95 case 'LaunchCalculator': return 'LaunchApplication2';
98 // iOS leaks some OS names
100 case 'UIKeyInputUpArrow': return 'ArrowUp';
101 case 'UIKeyInputDownArrow': return 'ArrowDown';
102 case 'UIKeyInputLeftArrow': return 'ArrowLeft';
103 case 'UIKeyInputRightArrow': return 'ArrowRight';
104 case 'UIKeyInputEscape': return 'Escape';
107 // Broken behaviour in Chrome
108 if ((evt
.key
=== '\x00') && (evt
.code
=== 'NumpadDecimal')) {
115 // Try to deduce it based on the physical key
116 const code
= getKeycode(evt
);
117 if (code
in fixedkeys
) {
118 return fixedkeys
[code
];
121 // If that failed, then see if we have a printable character
123 return String
.fromCharCode(evt
.charCode
);
126 // At this point we have nothing left to go on
127 return 'Unidentified';
130 // Get the most reliable keysym value we can get from a key event
131 export function getKeysym(evt
) {
132 const key
= getKey(evt
);
134 if (key
=== 'Unidentified') {
138 // First look up special keys
139 if (key
in DOMKeyTable
) {
140 let location
= evt
.location
;
142 // Safari screws up location for the right cmd key
143 if ((key
=== 'Meta') && (location
=== 0)) {
148 if ((key
=== 'Clear') && (location
=== 3)) {
149 let code
= getKeycode(evt
);
150 if (code
=== 'NumLock') {
155 if ((location
=== undefined) || (location
> 3)) {
159 // The original Meta key now gets confused with the Windows key
160 // https://bugs.chromium.org/p/chromium/issues/detail?id=1020141
161 // https://bugzilla.mozilla.org/show_bug.cgi?id=1232918
162 if (key
=== 'Meta') {
163 let code
= getKeycode(evt
);
164 if (code
=== 'AltLeft') {
165 return KeyTable
.XK_Meta_L
;
166 } else if (code
=== 'AltRight') {
167 return KeyTable
.XK_Meta_R
;
171 // macOS has Clear instead of NumLock, but the remote system is
172 // probably not macOS, so lying here is probably best...
173 if (key
=== 'Clear') {
174 let code
= getKeycode(evt
);
175 if (code
=== 'NumLock') {
176 return KeyTable
.XK_Num_Lock
;
180 return DOMKeyTable
[key
][location
];
183 // Now we need to look at the Unicode symbol instead
185 // Special key? (FIXME: Should have been caught earlier)
186 if (key
.length
!== 1) {
190 const codepoint
= key
.charCodeAt();
192 return keysyms
.lookup(codepoint
);