]> git.proxmox.com Git - mirror_novnc.git/blob - core/input/util.js
Move browser checks to browser.js
[mirror_novnc.git] / 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";
7
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
13 // undefined)
14 if (evt.code) {
15 // Mozilla isn't fully in sync with the spec yet
16 switch (evt.code) {
17 case 'OSLeft': return 'MetaLeft';
18 case 'OSRight': return 'MetaRight';
19 }
20
21 return evt.code;
22 }
23
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];
29
30 // macOS has messed up this code for some reason
31 if (browser.isMac() && (code === 'ContextMenu')) {
32 code = 'MetaRight';
33 }
34
35 // The keyCode doesn't distinguish between left and right
36 // for the standard modifiers
37 if (evt.location === 2) {
38 switch (code) {
39 case 'ShiftLeft': return 'ShiftRight';
40 case 'ControlLeft': return 'ControlRight';
41 case 'AltLeft': return 'AltRight';
42 }
43 }
44
45 // Nor a bunch of the numpad keys
46 if (evt.location === 3) {
47 switch (code) {
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';
59 }
60 }
61
62 return code;
63 }
64
65 return 'Unidentified';
66 }
67
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/
74 switch (evt.key) {
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;
90 }
91
92 // Mozilla isn't fully in sync with the spec yet
93 switch (evt.key) {
94 case 'OS': return 'Meta';
95 }
96
97 // iOS leaks some OS names
98 switch (evt.key) {
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';
104 }
105
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())) {
109 return evt.key;
110 }
111 }
112
113 // Try to deduce it based on the physical key
114 var code = getKeycode(evt);
115 if (code in fixedkeys) {
116 return fixedkeys[code];
117 }
118
119 // If that failed, then see if we have a printable character
120 if (evt.charCode) {
121 return String.fromCharCode(evt.charCode);
122 }
123
124 // At this point we have nothing left to go on
125 return 'Unidentified';
126 }
127
128 // Get the most reliable keysym value we can get from a key event
129 export function getKeysym(evt){
130 var key = getKey(evt);
131
132 if (key === 'Unidentified') {
133 return null;
134 }
135
136 // First look up special keys
137 if (key in DOMKeyTable) {
138 var location = evt.location;
139
140 // Safari screws up location for the right cmd key
141 if ((key === 'Meta') && (location === 0)) {
142 location = 2;
143 }
144
145 if ((location === undefined) || (location > 3)) {
146 location = 0;
147 }
148
149 return DOMKeyTable[key][location];
150 }
151
152 // Now we need to look at the Unicode symbol instead
153
154 var codepoint;
155
156 // Special key? (FIXME: Should have been caught earlier)
157 if (key.length !== 1) {
158 return null;
159 }
160
161 codepoint = key.charCodeAt();
162 if (codepoint) {
163 return keysyms.lookup(codepoint);
164 }
165
166 return null;
167 }