]>
git.proxmox.com Git - mirror_novnc.git/blob - core/input/keyboard.js
2 * noVNC: HTML5 VNC client
3 * Copyright (C) 2012 Joel Martin
4 * Copyright (C) 2013 Samuel Mannehed for Cendio AB
5 * Licensed under MPL 2.0 or any later version (see LICENSE.txt)
8 import * as Log
from '../util/logging.js';
9 import { stopEvent
} from '../util/events.js';
10 import * as KeyboardUtil
from "./util.js";
11 import KeyTable
from "./keysym.js";
12 import * as browser
from "../util/browser.js";
15 // Keyboard event handler
18 export default class Keyboard
{
20 this._target
= target
|| null;
22 this._keyDownList
= {}; // List of depressed keys
23 // (even if they are happy)
24 this._pendingKey
= null; // Key waiting for keypress
25 this._altGrArmed
= false; // Windows AltGr detection
27 // keep these here so we can refer to them later
28 this._eventHandlers
= {
29 'keyup': this._handleKeyUp
.bind(this),
30 'keydown': this._handleKeyDown
.bind(this),
31 'keypress': this._handleKeyPress
.bind(this),
32 'blur': this._allKeysUp
.bind(this),
33 'checkalt': this._checkAlt
.bind(this),
36 // ===== EVENT HANDLERS =====
38 this.onkeyevent
= () => {}; // Handler for key press/release
41 // ===== PRIVATE METHODS =====
43 _sendKeyEvent(keysym
, code
, down
) {
45 this._keyDownList
[code
] = keysym
;
47 // Do we really think this key is down?
48 if (!(code
in this._keyDownList
)) {
51 delete this._keyDownList
[code
];
54 Log
.Debug("onkeyevent " + (down
? "down" : "up") +
55 ", keysym: " + keysym
, ", code: " + code
);
56 this.onkeyevent(keysym
, code
, down
);
60 const code
= KeyboardUtil
.getKeycode(e
);
61 if (code
!== 'Unidentified') {
65 // Unstable, but we don't have anything else to go on
66 // (don't use it for 'keypress' events thought since
67 // WebKit sets it to the same as charCode)
68 if (e
.keyCode
&& (e
.type
!== 'keypress')) {
69 // 229 is used for composition events
70 if (e
.keyCode
!== 229) {
71 return 'Platform' + e
.keyCode
;
75 // A precursor to the final DOM3 standard. Unfortunately it
76 // is not layout independent, so it is as bad as using keyCode
77 if (e
.keyIdentifier
) {
79 if (e
.keyIdentifier
.substr(0, 2) !== 'U+') {
80 return e
.keyIdentifier
;
83 const codepoint
= parseInt(e
.keyIdentifier
.substr(2), 16);
84 const char = String
.fromCharCode(codepoint
).toUpperCase();
86 return 'Platform' + char.charCodeAt();
89 return 'Unidentified';
93 const code
= this._getKeyCode(e
);
94 let keysym
= KeyboardUtil
.getKeysym(e
);
96 // Windows doesn't have a proper AltGr, but handles it using
97 // fake Ctrl+Alt. However the remote end might not be Windows,
98 // so we need to merge those in to a single AltGr event. We
99 // detect this case by seeing the two key events directly after
100 // each other with a very short time between them (<50ms).
101 if (this._altGrArmed
) {
102 this._altGrArmed
= false;
103 clearTimeout(this._altGrTimeout
);
105 if ((code
=== "AltRight") &&
106 ((e
.timeStamp
- this._altGrCtrlTime
) < 50)) {
107 // FIXME: We fail to detect this if either Ctrl key is
108 // first manually pressed as Windows then no
109 // longer sends the fake Ctrl down event. It
110 // does however happily send real Ctrl events
111 // even when AltGr is already down. Some
112 // browsers detect this for us though and set the
113 // key to "AltGraph".
114 keysym
= KeyTable
.XK_ISO_Level3_Shift
;
116 this._sendKeyEvent(KeyTable
.XK_Control_L
, "ControlLeft", true);
120 // We cannot handle keys we cannot track, but we also need
121 // to deal with virtual keyboards which omit key info
122 // (iOS omits tracking info on keyup events, which forces us to
123 // special treat that platform here)
124 if ((code
=== 'Unidentified') || browser
.isIOS()) {
126 // If it's a virtual keyboard then it should be
127 // sufficient to just send press and release right
129 this._sendKeyEvent(keysym
, code
, true);
130 this._sendKeyEvent(keysym
, code
, false);
137 // Alt behaves more like AltGraph on macOS, so shuffle the
138 // keys around a bit to make things more sane for the remote
139 // server. This method is used by RealVNC and TigerVNC (and
141 if (browser
.isMac()) {
143 case KeyTable
.XK_Super_L
:
144 keysym
= KeyTable
.XK_Alt_L
;
146 case KeyTable
.XK_Super_R
:
147 keysym
= KeyTable
.XK_Super_L
;
149 case KeyTable
.XK_Alt_L
:
150 keysym
= KeyTable
.XK_Mode_switch
;
152 case KeyTable
.XK_Alt_R
:
153 keysym
= KeyTable
.XK_ISO_Level3_Shift
;
158 // Is this key already pressed? If so, then we must use the
159 // same keysym or we'll confuse the server
160 if (code
in this._keyDownList
) {
161 keysym
= this._keyDownList
[code
];
164 // macOS doesn't send proper key events for modifiers, only
165 // state change events. That gets extra confusing for CapsLock
166 // which toggles on each press, but not on release. So pretend
167 // it was a quick press and release of the button.
168 if (browser
.isMac() && (code
=== 'CapsLock')) {
169 this._sendKeyEvent(KeyTable
.XK_Caps_Lock
, 'CapsLock', true);
170 this._sendKeyEvent(KeyTable
.XK_Caps_Lock
, 'CapsLock', false);
175 // If this is a legacy browser then we'll need to wait for
176 // a keypress event as well
177 // (IE and Edge has a broken KeyboardEvent.key, so we can't
178 // just check for the presence of that field)
179 if (!keysym
&& (!e
.key
|| browser
.isIE() || browser
.isEdge())) {
180 this._pendingKey
= code
;
181 // However we might not get a keypress event if the key
182 // is non-printable, which needs some special fallback
184 setTimeout(this._handleKeyPressTimeout
.bind(this), 10, e
);
188 this._pendingKey
= null;
191 // Possible start of AltGr sequence? (see above)
192 if ((code
=== "ControlLeft") && browser
.isWindows() &&
193 !("ControlLeft" in this._keyDownList
)) {
194 this._altGrArmed
= true;
195 this._altGrTimeout
= setTimeout(this._handleAltGrTimeout
.bind(this), 100);
196 this._altGrCtrlTime
= e
.timeStamp
;
200 this._sendKeyEvent(keysym
, code
, true);
203 // Legacy event for browsers without code/key
207 // Are we expecting a keypress?
208 if (this._pendingKey
=== null) {
212 let code
= this._getKeyCode(e
);
213 const keysym
= KeyboardUtil
.getKeysym(e
);
215 // The key we were waiting for?
216 if ((code
!== 'Unidentified') && (code
!= this._pendingKey
)) {
220 code
= this._pendingKey
;
221 this._pendingKey
= null;
224 Log
.Info('keypress with no keysym:', e
);
228 this._sendKeyEvent(keysym
, code
, true);
231 _handleKeyPressTimeout(e
) {
232 // Did someone manage to sort out the key already?
233 if (this._pendingKey
=== null) {
239 const code
= this._pendingKey
;
240 this._pendingKey
= null;
242 // We have no way of knowing the proper keysym with the
243 // information given, but the following are true for most
245 if ((e
.keyCode
>= 0x30) && (e
.keyCode
<= 0x39)) {
248 } else if ((e
.keyCode
>= 0x41) && (e
.keyCode
<= 0x5a)) {
250 let char = String
.fromCharCode(e
.keyCode
);
251 // A feeble attempt at the correct case
253 char = char.toUpperCase();
255 char = char.toLowerCase();
256 keysym
= char.charCodeAt();
262 this._sendKeyEvent(keysym
, code
, true);
268 const code
= this._getKeyCode(e
);
270 // We can't get a release in the middle of an AltGr sequence, so
271 // abort that detection
272 if (this._altGrArmed
) {
273 this._altGrArmed
= false;
274 clearTimeout(this._altGrTimeout
);
275 this._sendKeyEvent(KeyTable
.XK_Control_L
, "ControlLeft", true);
278 // See comment in _handleKeyDown()
279 if (browser
.isMac() && (code
=== 'CapsLock')) {
280 this._sendKeyEvent(KeyTable
.XK_Caps_Lock
, 'CapsLock', true);
281 this._sendKeyEvent(KeyTable
.XK_Caps_Lock
, 'CapsLock', false);
285 this._sendKeyEvent(this._keyDownList
[code
], code
, false);
288 _handleAltGrTimeout() {
289 this._altGrArmed
= false;
290 clearTimeout(this._altGrTimeout
);
291 this._sendKeyEvent(KeyTable
.XK_Control_L
, "ControlLeft", true);
295 Log
.Debug(">> Keyboard.allKeysUp");
296 for (let code
in this._keyDownList
) {
297 this._sendKeyEvent(this._keyDownList
[code
], code
, false);
299 Log
.Debug("<< Keyboard.allKeysUp");
302 // Firefox Alt workaround, see below
308 const target
= this._target
;
309 const downList
= this._keyDownList
;
310 ['AltLeft', 'AltRight'].forEach((code
) => {
311 if (!(code
in downList
)) {
315 const event
= new KeyboardEvent('keyup',
316 { key
: downList
[code
],
318 target
.dispatchEvent(event
);
322 // ===== PUBLIC METHODS =====
325 //Log.Debug(">> Keyboard.grab");
327 this._target
.addEventListener('keydown', this._eventHandlers
.keydown
);
328 this._target
.addEventListener('keyup', this._eventHandlers
.keyup
);
329 this._target
.addEventListener('keypress', this._eventHandlers
.keypress
);
331 // Release (key up) if window loses focus
332 window
.addEventListener('blur', this._eventHandlers
.blur
);
334 // Firefox has broken handling of Alt, so we need to poll as
335 // best we can for releases (still doesn't prevent the menu
336 // from popping up though as we can't call preventDefault())
337 if (browser
.isWindows() && browser
.isFirefox()) {
338 const handler
= this._eventHandlers
.checkalt
;
339 ['mousedown', 'mouseup', 'mousemove', 'wheel',
340 'touchstart', 'touchend', 'touchmove',
341 'keydown', 'keyup'].forEach(type
=>
342 document
.addEventListener(type
, handler
,
347 //Log.Debug("<< Keyboard.grab");
351 //Log.Debug(">> Keyboard.ungrab");
353 if (browser
.isWindows() && browser
.isFirefox()) {
354 const handler
= this._eventHandlers
.checkalt
;
355 ['mousedown', 'mouseup', 'mousemove', 'wheel',
356 'touchstart', 'touchend', 'touchmove',
357 'keydown', 'keyup'].forEach(type
=> document
.removeEventListener(type
, handler
));
360 this._target
.removeEventListener('keydown', this._eventHandlers
.keydown
);
361 this._target
.removeEventListener('keyup', this._eventHandlers
.keyup
);
362 this._target
.removeEventListener('keypress', this._eventHandlers
.keypress
);
363 window
.removeEventListener('blur', this._eventHandlers
.blur
);
365 // Release (key up) all keys that are in a down state
368 //Log.Debug(">> Keyboard.ungrab");