]>
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 /*jslint browser: true, white: false */
9 /*global window, Util */
11 import * as Log
from '../util/logging.js';
12 import { stopEvent
} from '../util/events.js';
13 import * as KeyboardUtil
from "./util.js";
14 import KeyTable
from "./keysym.js";
17 // Keyboard event handler
20 export default function Keyboard(target
) {
21 this._target
= target
|| null;
23 this._keyDownList
= {}; // List of depressed keys
24 // (even if they are happy)
25 this._pendingKey
= null; // Key waiting for keypress
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)
37 return navigator
&& !!(/mac/i).exec(navigator
.platform
);
39 function isWindows() {
40 return navigator
&& !!(/win/i).exec(navigator
.platform
);
44 (!!(/ipad/i).exec(navigator
.platform
) ||
45 !!(/iphone/i).exec(navigator
.platform
) ||
46 !!(/ipod/i).exec(navigator
.platform
));
49 return navigator
&& !!(/trident/i).exec(navigator
.userAgent
);
52 return navigator
&& !!(/edge/i).exec(navigator
.userAgent
);
55 Keyboard
.prototype = {
56 // ===== EVENT HANDLERS =====
58 onkeyevent: function () {}, // Handler for key press/release
60 // ===== PRIVATE METHODS =====
62 _sendKeyEvent: function (keysym
, code
, down
) {
63 Log
.Debug("onkeyevent " + (down
? "down" : "up") +
64 ", keysym: " + keysym
, ", code: " + code
);
66 // Windows sends CtrlLeft+AltRight when you press
67 // AltGraph, which tends to confuse the hell out of
68 // remote systems. Fake a release of these keys until
69 // there is a way to detect AltGraph properly.
70 var fakeAltGraph
= false;
71 if (down
&& isWindows()) {
72 if ((code
!== 'ControlLeft') &&
73 (code
!== 'AltRight') &&
74 ('ControlLeft' in this._keyDownList
) &&
75 ('AltRight' in this._keyDownList
)) {
77 this.onkeyevent(this._keyDownList
['AltRight'],
79 this.onkeyevent(this._keyDownList
['ControlLeft'],
80 'ControlLeft', false);
84 this.onkeyevent(keysym
, code
, down
);
87 this.onkeyevent(this._keyDownList
['ControlLeft'],
89 this.onkeyevent(this._keyDownList
['AltRight'],
94 _getKeyCode: function (e
) {
95 var code
= KeyboardUtil
.getKeycode(e
);
96 if (code
!== 'Unidentified') {
100 // Unstable, but we don't have anything else to go on
101 // (don't use it for 'keypress' events thought since
102 // WebKit sets it to the same as charCode)
103 if (e
.keyCode
&& (e
.type
!== 'keypress')) {
104 // 229 is used for composition events
105 if (e
.keyCode
!== 229) {
106 return 'Platform' + e
.keyCode
;
110 // A precursor to the final DOM3 standard. Unfortunately it
111 // is not layout independent, so it is as bad as using keyCode
112 if (e
.keyIdentifier
) {
113 // Non-character key?
114 if (e
.keyIdentifier
.substr(0, 2) !== 'U+') {
115 return e
.keyIdentifier
;
118 var codepoint
= parseInt(e
.keyIdentifier
.substr(2), 16);
119 var char = String
.fromCharCode(codepoint
);
120 // Some implementations fail to uppercase the symbols
121 char = char.toUpperCase();
123 return 'Platform' + char.charCodeAt();
126 return 'Unidentified';
129 _handleKeyDown: function (e
) {
130 var code
= this._getKeyCode(e
);
131 var keysym
= KeyboardUtil
.getKeysym(e
);
133 // We cannot handle keys we cannot track, but we also need
134 // to deal with virtual keyboards which omit key info
135 // (iOS omits tracking info on keyup events, which forces us to
136 // special treat that platform here)
137 if ((code
=== 'Unidentified') || isIOS()) {
139 // If it's a virtual keyboard then it should be
140 // sufficient to just send press and release right
142 this._sendKeyEvent(keysym
, code
, true);
143 this._sendKeyEvent(keysym
, code
, false);
150 // Alt behaves more like AltGraph on macOS, so shuffle the
151 // keys around a bit to make things more sane for the remote
152 // server. This method is used by RealVNC and TigerVNC (and
156 case KeyTable
.XK_Super_L
:
157 keysym
= KeyTable
.XK_Alt_L
;
159 case KeyTable
.XK_Super_R
:
160 keysym
= KeyTable
.XK_Super_L
;
162 case KeyTable
.XK_Alt_L
:
163 keysym
= KeyTable
.XK_Mode_switch
;
165 case KeyTable
.XK_Alt_R
:
166 keysym
= KeyTable
.XK_ISO_Level3_Shift
;
171 // Is this key already pressed? If so, then we must use the
172 // same keysym or we'll confuse the server
173 if (code
in this._keyDownList
) {
174 keysym
= this._keyDownList
[code
];
177 // macOS doesn't send proper key events for modifiers, only
178 // state change events. That gets extra confusing for CapsLock
179 // which toggles on each press, but not on release. So pretend
180 // it was a quick press and release of the button.
181 if (isMac() && (code
=== 'CapsLock')) {
182 this._sendKeyEvent(KeyTable
.XK_Caps_Lock
, 'CapsLock', true);
183 this._sendKeyEvent(KeyTable
.XK_Caps_Lock
, 'CapsLock', false);
188 // If this is a legacy browser then we'll need to wait for
189 // a keypress event as well
190 // (IE and Edge has a broken KeyboardEvent.key, so we can't
191 // just check for the presence of that field)
192 if (!keysym
&& (!e
.key
|| isIE() || isEdge())) {
193 this._pendingKey
= code
;
194 // However we might not get a keypress event if the key
195 // is non-printable, which needs some special fallback
197 setTimeout(this._handleKeyPressTimeout
.bind(this), 10, e
);
201 this._pendingKey
= null;
204 this._keyDownList
[code
] = keysym
;
206 this._sendKeyEvent(keysym
, code
, true);
209 // Legacy event for browsers without code/key
210 _handleKeyPress: function (e
) {
213 // Are we expecting a keypress?
214 if (this._pendingKey
=== null) {
218 var code
= this._getKeyCode(e
);
219 var keysym
= KeyboardUtil
.getKeysym(e
);
221 // The key we were waiting for?
222 if ((code
!== 'Unidentified') && (code
!= this._pendingKey
)) {
226 code
= this._pendingKey
;
227 this._pendingKey
= null;
230 Log
.Info('keypress with no keysym:', e
);
234 this._keyDownList
[code
] = keysym
;
236 this._sendKeyEvent(keysym
, code
, true);
238 _handleKeyPressTimeout: function (e
) {
239 // Did someone manage to sort out the key already?
240 if (this._pendingKey
=== null) {
246 code
= this._pendingKey
;
247 this._pendingKey
= null;
249 // We have no way of knowing the proper keysym with the
250 // information given, but the following are true for most
252 if ((e
.keyCode
>= 0x30) && (e
.keyCode
<= 0x39)) {
255 } else if ((e
.keyCode
>= 0x41) && (e
.keyCode
<= 0x5a)) {
257 var char = String
.fromCharCode(e
.keyCode
);
258 // A feeble attempt at the correct case
260 char = char.toUpperCase();
262 char = char.toLowerCase();
263 keysym
= char.charCodeAt();
269 this._keyDownList
[code
] = keysym
;
271 this._sendKeyEvent(keysym
, code
, true);
274 _handleKeyUp: function (e
) {
277 var code
= this._getKeyCode(e
);
279 // See comment in _handleKeyDown()
280 if (isMac() && (code
=== 'CapsLock')) {
281 this._sendKeyEvent(KeyTable
.XK_Caps_Lock
, 'CapsLock', true);
282 this._sendKeyEvent(KeyTable
.XK_Caps_Lock
, 'CapsLock', false);
286 // Do we really think this key is down?
287 if (!(code
in this._keyDownList
)) {
291 this._sendKeyEvent(this._keyDownList
[code
], code
, false);
293 delete this._keyDownList
[code
];
296 _allKeysUp: function () {
297 Log
.Debug(">> Keyboard.allKeysUp");
298 for (var code
in this._keyDownList
) {
299 this._sendKeyEvent(this._keyDownList
[code
], code
, false);
301 this._keyDownList
= {};
302 Log
.Debug("<< Keyboard.allKeysUp");
305 // ===== PUBLIC METHODS =====
308 //Log.Debug(">> Keyboard.grab");
309 var c
= this._target
;
311 c
.addEventListener('keydown', this._eventHandlers
.keydown
);
312 c
.addEventListener('keyup', this._eventHandlers
.keyup
);
313 c
.addEventListener('keypress', this._eventHandlers
.keypress
);
315 // Release (key up) if window loses focus
316 window
.addEventListener('blur', this._eventHandlers
.blur
);
318 //Log.Debug("<< Keyboard.grab");
321 ungrab: function () {
322 //Log.Debug(">> Keyboard.ungrab");
323 var c
= this._target
;
325 c
.removeEventListener('keydown', this._eventHandlers
.keydown
);
326 c
.removeEventListener('keyup', this._eventHandlers
.keyup
);
327 c
.removeEventListener('keypress', this._eventHandlers
.keypress
);
328 window
.removeEventListener('blur', this._eventHandlers
.blur
);
330 // Release (key up) all keys that are in a down state
333 //Log.Debug(">> Keyboard.ungrab");