]>
git.proxmox.com Git - mirror_novnc.git/blob - core/input/util.js
1 import keysyms
from "./keysymdef.js";
2 import vkeys
from "./vkeys.js";
3 import fixedkeys
from "./fixedkeys.js";
4 import DOMKeyTable
from "./domkeytable.js";
5 import * as browser
from "../util/browser.js";
7 // Get 'KeyboardEvent.code', handling legacy browsers
8 export function getKeycode(evt
) {
9 // Are we getting proper key identifiers?
10 // (unfortunately Firefox and Chrome are crappy here and gives
11 // us an empty string on some platforms, rather than leaving it
14 // Mozilla isn't fully in sync with the spec yet
16 case 'OSLeft': return 'MetaLeft';
17 case 'OSRight': return 'MetaRight';
23 // The de-facto standard is to use Windows Virtual-Key codes
24 // in the 'keyCode' field for non-printable characters. However
25 // Webkit sets it to the same as charCode in 'keypress' events.
26 if ((evt
.type
!== 'keypress') && (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';
96 // iOS leaks some OS names
98 case 'UIKeyInputUpArrow': return 'ArrowUp';
99 case 'UIKeyInputDownArrow': return 'ArrowDown';
100 case 'UIKeyInputLeftArrow': return 'ArrowLeft';
101 case 'UIKeyInputRightArrow': return 'ArrowRight';
102 case 'UIKeyInputEscape': return 'Escape';
105 // IE and Edge have broken handling of AltGraph so we cannot
106 // trust them for printable characters
107 if ((evt
.key
.length
!== 1) || (!browser
.isIE() && !browser
.isEdge())) {
112 // Try to deduce it based on the physical key
113 const code
= getKeycode(evt
);
114 if (code
in fixedkeys
) {
115 return fixedkeys
[code
];
118 // If that failed, then see if we have a printable character
120 return String
.fromCharCode(evt
.charCode
);
123 // At this point we have nothing left to go on
124 return 'Unidentified';
127 // Get the most reliable keysym value we can get from a key event
128 export function getKeysym(evt
) {
129 const key
= getKey(evt
);
131 if (key
=== 'Unidentified') {
135 // First look up special keys
136 if (key
in DOMKeyTable
) {
137 let location
= evt
.location
;
139 // Safari screws up location for the right cmd key
140 if ((key
=== 'Meta') && (location
=== 0)) {
144 if ((location
=== undefined) || (location
> 3)) {
148 return DOMKeyTable
[key
][location
];
151 // Now we need to look at the Unicode symbol instead
153 // Special key? (FIXME: Should have been caught earlier)
154 if (key
.length
!== 1) {
158 const codepoint
= key
.charCodeAt();
160 return keysyms
.lookup(codepoint
);