]>
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";
8 return navigator
&& !!(/mac/i).exec(navigator
.platform
);
11 return navigator
&& !!(/trident/i).exec(navigator
.userAgent
);
14 return navigator
&& !!(/edge/i).exec(navigator
.userAgent
);
17 // Get 'KeyboardEvent.code', handling legacy browsers
18 export function getKeycode(evt
){
19 // Are we getting proper key identifiers?
20 // (unfortunately Firefox and Chrome are crappy here and gives
21 // us an empty string on some platforms, rather than leaving it
24 // Mozilla isn't fully in sync with the spec yet
26 case 'OSLeft': return 'MetaLeft';
27 case 'OSRight': return 'MetaRight';
33 // The de-facto standard is to use Windows Virtual-Key codes
34 // in the 'keyCode' field for non-printable characters. However
35 // Webkit sets it to the same as charCode in 'keypress' events.
36 if ((evt
.type
!== 'keypress') && (evt
.keyCode
in vkeys
)) {
37 var code
= vkeys
[evt
.keyCode
];
39 // macOS has messed up this code for some reason
40 if (isMac() && (code
=== 'ContextMenu')) {
44 // The keyCode doesn't distinguish between left and right
45 // for the standard modifiers
46 if (evt
.location
=== 2) {
48 case 'ShiftLeft': return 'ShiftRight';
49 case 'ControlLeft': return 'ControlRight';
50 case 'AltLeft': return 'AltRight';
54 // Nor a bunch of the numpad keys
55 if (evt
.location
=== 3) {
57 case 'Delete': return 'NumpadDecimal';
58 case 'Insert': return 'Numpad0';
59 case 'End': return 'Numpad1';
60 case 'ArrowDown': return 'Numpad2';
61 case 'PageDown': return 'Numpad3';
62 case 'ArrowLeft': return 'Numpad4';
63 case 'ArrowRight': return 'Numpad6';
64 case 'Home': return 'Numpad7';
65 case 'ArrowUp': return 'Numpad8';
66 case 'PageUp': return 'Numpad9';
67 case 'Enter': return 'NumpadEnter';
74 return 'Unidentified';
77 // Get 'KeyboardEvent.key', handling legacy browsers
78 export function getKey(evt
) {
79 // Are we getting a proper key value?
80 if (evt
.key
!== undefined) {
81 // IE and Edge use some ancient version of the spec
82 // https://developer.microsoft.com/en-us/microsoft-edge/platform/issues/8860571/
84 case 'Spacebar': return ' ';
85 case 'Esc': return 'Escape';
86 case 'Scroll': return 'ScrollLock';
87 case 'Win': return 'Meta';
88 case 'Apps': return 'ContextMenu';
89 case 'Up': return 'ArrowUp';
90 case 'Left': return 'ArrowLeft';
91 case 'Right': return 'ArrowRight';
92 case 'Down': return 'ArrowDown';
93 case 'Del': return 'Delete';
94 case 'Divide': return '/';
95 case 'Multiply': return '*';
96 case 'Subtract': return '-';
97 case 'Add': return '+';
98 case 'Decimal': return evt
.char;
101 // Mozilla isn't fully in sync with the spec yet
103 case 'OS': return 'Meta';
106 // IE and Edge have broken handling of AltGraph so we cannot
107 // trust them for printable characters
108 if ((evt
.key
.length
!== 1) || (!isIE() && !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
);