]>
git.proxmox.com Git - mirror_novnc.git/blob - core/input/keyboard.js
d64298a8dd93e228cf8a53ca7bd0cbc483a261dc
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 function Keyboard(target
) {
19 this._target
= target
|| null;
21 this._keyDownList
= {}; // List of depressed keys
22 // (even if they are happy)
23 this._pendingKey
= null; // Key waiting for keypress
24 this._altGrArmed
= false; // Windows AltGr detection
26 // keep these here so we can refer to them later
27 this._eventHandlers
= {
28 'keyup': this._handleKeyUp
.bind(this),
29 'keydown': this._handleKeyDown
.bind(this),
30 'keypress': this._handleKeyPress
.bind(this),
31 'blur': this._allKeysUp
.bind(this),
32 'checkalt': this._checkAlt
.bind(this),
36 Keyboard
.prototype = {
37 // ===== EVENT HANDLERS =====
39 onkeyevent: function () {}, // Handler for key press/release
41 // ===== PRIVATE METHODS =====
43 _sendKeyEvent: function (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
);
59 _getKeyCode: function (e
) {
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';
92 _handleKeyDown: function (e
) {
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
204 _handleKeyPress: function (e
) {
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);
230 _handleKeyPressTimeout: function (e
) {
231 // Did someone manage to sort out the key already?
232 if (this._pendingKey
=== null) {
238 const code
= this._pendingKey
;
239 this._pendingKey
= null;
241 // We have no way of knowing the proper keysym with the
242 // information given, but the following are true for most
244 if ((e
.keyCode
>= 0x30) && (e
.keyCode
<= 0x39)) {
247 } else if ((e
.keyCode
>= 0x41) && (e
.keyCode
<= 0x5a)) {
249 let char = String
.fromCharCode(e
.keyCode
);
250 // A feeble attempt at the correct case
252 char = char.toUpperCase();
254 char = char.toLowerCase();
255 keysym
= char.charCodeAt();
261 this._sendKeyEvent(keysym
, code
, true);
264 _handleKeyUp: function (e
) {
267 const code
= this._getKeyCode(e
);
269 // We can't get a release in the middle of an AltGr sequence, so
270 // abort that detection
271 if (this._altGrArmed
) {
272 this._altGrArmed
= false;
273 clearTimeout(this._altGrTimeout
);
274 this._sendKeyEvent(KeyTable
.XK_Control_L
, "ControlLeft", true);
277 // See comment in _handleKeyDown()
278 if (browser
.isMac() && (code
=== 'CapsLock')) {
279 this._sendKeyEvent(KeyTable
.XK_Caps_Lock
, 'CapsLock', true);
280 this._sendKeyEvent(KeyTable
.XK_Caps_Lock
, 'CapsLock', false);
284 this._sendKeyEvent(this._keyDownList
[code
], code
, false);
287 _handleAltGrTimeout: function () {
288 this._altGrArmed
= false;
289 clearTimeout(this._altGrTimeout
);
290 this._sendKeyEvent(KeyTable
.XK_Control_L
, "ControlLeft", true);
293 _allKeysUp: function () {
294 Log
.Debug(">> Keyboard.allKeysUp");
295 for (let code
in this._keyDownList
) {
296 this._sendKeyEvent(this._keyDownList
[code
], code
, false);
298 Log
.Debug("<< Keyboard.allKeysUp");
301 // Firefox Alt workaround, see below
302 _checkAlt: function (e
) {
307 const target
= this._target
;
308 const downList
= this._keyDownList
;
309 ['AltLeft', 'AltRight'].forEach(function (code
) {
310 if (!(code
in downList
)) {
314 const event
= new KeyboardEvent('keyup',
315 { key
: downList
[code
],
317 target
.dispatchEvent(event
);
321 // ===== PUBLIC METHODS =====
324 //Log.Debug(">> Keyboard.grab");
326 this._target
.addEventListener('keydown', this._eventHandlers
.keydown
);
327 this._target
.addEventListener('keyup', this._eventHandlers
.keyup
);
328 this._target
.addEventListener('keypress', this._eventHandlers
.keypress
);
330 // Release (key up) if window loses focus
331 window
.addEventListener('blur', this._eventHandlers
.blur
);
333 // Firefox has broken handling of Alt, so we need to poll as
334 // best we can for releases (still doesn't prevent the menu
335 // from popping up though as we can't call preventDefault())
336 if (browser
.isWindows() && browser
.isFirefox()) {
337 const handler
= this._eventHandlers
.checkalt
;
338 ['mousedown', 'mouseup', 'mousemove', 'wheel',
339 'touchstart', 'touchend', 'touchmove',
340 'keydown', 'keyup'].forEach(function (type
) {
341 document
.addEventListener(type
, handler
,
347 //Log.Debug("<< Keyboard.grab");
350 ungrab: function () {
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(function (type
) {
358 document
.removeEventListener(type
, handler
);
362 this._target
.removeEventListener('keydown', this._eventHandlers
.keydown
);
363 this._target
.removeEventListener('keyup', this._eventHandlers
.keyup
);
364 this._target
.removeEventListener('keypress', this._eventHandlers
.keypress
);
365 window
.removeEventListener('blur', this._eventHandlers
.blur
);
367 // Release (key up) all keys that are in a down state
370 //Log.Debug(">> Keyboard.ungrab");