]>
git.proxmox.com Git - mirror_novnc.git/blob - core/input/keyboard.js
fa4a5ae4f0ff1c2ac3a6583dda89106edc02de44
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 { set_defaults
, make_properties
} from '../util/properties.js';
14 import * as KeyboardUtil
from "./util.js";
15 import KeyTable
from "./keysym.js";
18 // Keyboard event handler
21 export default function Keyboard(defaults
) {
22 this._keyDownList
= {}; // List of depressed keys
23 // (even if they are happy)
24 this._pendingKey
= null; // Key waiting for keypress
26 set_defaults(this, defaults
, {
30 // keep these here so we can refer to them later
31 this._eventHandlers
= {
32 'keyup': this._handleKeyUp
.bind(this),
33 'keydown': this._handleKeyDown
.bind(this),
34 'keypress': this._handleKeyPress
.bind(this),
35 'blur': this._allKeysUp
.bind(this)
40 return navigator
&& !!(/mac/i).exec(navigator
.platform
);
42 function isWindows() {
43 return navigator
&& !!(/win/i).exec(navigator
.platform
);
47 (!!(/ipad/i).exec(navigator
.platform
) ||
48 !!(/iphone/i).exec(navigator
.platform
) ||
49 !!(/ipod/i).exec(navigator
.platform
));
52 return navigator
&& !!(/trident/i).exec(navigator
.userAgent
);
55 return navigator
&& !!(/edge/i).exec(navigator
.userAgent
);
58 Keyboard
.prototype = {
61 _sendKeyEvent: function (keysym
, code
, down
) {
62 if (!this._onKeyEvent
) {
66 Log
.Debug("onKeyEvent " + (down
? "down" : "up") +
67 ", keysym: " + keysym
, ", code: " + code
);
69 // Windows sends CtrlLeft+AltRight when you press
70 // AltGraph, which tends to confuse the hell out of
71 // remote systems. Fake a release of these keys until
72 // there is a way to detect AltGraph properly.
73 var fakeAltGraph
= false;
74 if (down
&& isWindows()) {
75 if ((code
!== 'ControlLeft') &&
76 (code
!== 'AltRight') &&
77 ('ControlLeft' in this._keyDownList
) &&
78 ('AltRight' in this._keyDownList
)) {
80 this._onKeyEvent(this._keyDownList
['AltRight'],
82 this._onKeyEvent(this._keyDownList
['ControlLeft'],
83 'ControlLeft', false);
87 this._onKeyEvent(keysym
, code
, down
);
90 this._onKeyEvent(this._keyDownList
['ControlLeft'],
92 this._onKeyEvent(this._keyDownList
['AltRight'],
97 _getKeyCode: function (e
) {
98 var code
= KeyboardUtil
.getKeycode(e
);
99 if (code
!== 'Unidentified') {
103 // Unstable, but we don't have anything else to go on
104 // (don't use it for 'keypress' events thought since
105 // WebKit sets it to the same as charCode)
106 if (e
.keyCode
&& (e
.type
!== 'keypress')) {
107 // 229 is used for composition events
108 if (e
.keyCode
!== 229) {
109 return 'Platform' + e
.keyCode
;
113 // A precursor to the final DOM3 standard. Unfortunately it
114 // is not layout independent, so it is as bad as using keyCode
115 if (e
.keyIdentifier
) {
116 // Non-character key?
117 if (e
.keyIdentifier
.substr(0, 2) !== 'U+') {
118 return e
.keyIdentifier
;
121 var codepoint
= parseInt(e
.keyIdentifier
.substr(2), 16);
122 var char = String
.fromCharCode(codepoint
);
123 // Some implementations fail to uppercase the symbols
124 char = char.toUpperCase();
126 return 'Platform' + char.charCodeAt();
129 return 'Unidentified';
132 _handleKeyDown: function (e
) {
133 var code
= this._getKeyCode(e
);
134 var keysym
= KeyboardUtil
.getKeysym(e
);
136 // We cannot handle keys we cannot track, but we also need
137 // to deal with virtual keyboards which omit key info
138 // (iOS omits tracking info on keyup events, which forces us to
139 // special treat that platform here)
140 if ((code
=== 'Unidentified') || isIOS()) {
142 // If it's a virtual keyboard then it should be
143 // sufficient to just send press and release right
145 this._sendKeyEvent(keysym
, code
, true);
146 this._sendKeyEvent(keysym
, code
, false);
153 // Alt behaves more like AltGraph on macOS, so shuffle the
154 // keys around a bit to make things more sane for the remote
155 // server. This method is used by RealVNC and TigerVNC (and
159 case KeyTable
.XK_Super_L
:
160 keysym
= KeyTable
.XK_Alt_L
;
162 case KeyTable
.XK_Super_R
:
163 keysym
= KeyTable
.XK_Super_L
;
165 case KeyTable
.XK_Alt_L
:
166 keysym
= KeyTable
.XK_Mode_switch
;
168 case KeyTable
.XK_Alt_R
:
169 keysym
= KeyTable
.XK_ISO_Level3_Shift
;
174 // Is this key already pressed? If so, then we must use the
175 // same keysym or we'll confuse the server
176 if (code
in this._keyDownList
) {
177 keysym
= this._keyDownList
[code
];
180 // macOS doesn't send proper key events for modifiers, only
181 // state change events. That gets extra confusing for CapsLock
182 // which toggles on each press, but not on release. So pretend
183 // it was a quick press and release of the button.
184 if (isMac() && (code
=== 'CapsLock')) {
185 this._sendKeyEvent(KeyTable
.XK_Caps_Lock
, 'CapsLock', true);
186 this._sendKeyEvent(KeyTable
.XK_Caps_Lock
, 'CapsLock', false);
191 // If this is a legacy browser then we'll need to wait for
192 // a keypress event as well
193 // (IE and Edge has a broken KeyboardEvent.key, so we can't
194 // just check for the presence of that field)
195 if (!keysym
&& (!e
.key
|| isIE() || isEdge())) {
196 this._pendingKey
= code
;
197 // However we might not get a keypress event if the key
198 // is non-printable, which needs some special fallback
200 setTimeout(this._handleKeyPressTimeout
.bind(this), 10, e
);
204 this._pendingKey
= null;
207 this._keyDownList
[code
] = keysym
;
209 this._sendKeyEvent(keysym
, code
, true);
212 // Legacy event for browsers without code/key
213 _handleKeyPress: function (e
) {
216 // Are we expecting a keypress?
217 if (this._pendingKey
=== null) {
221 var code
= this._getKeyCode(e
);
222 var keysym
= KeyboardUtil
.getKeysym(e
);
224 // The key we were waiting for?
225 if ((code
!== 'Unidentified') && (code
!= this._pendingKey
)) {
229 code
= this._pendingKey
;
230 this._pendingKey
= null;
233 console
.log('keypress with no keysym:', e
);
237 this._keyDownList
[code
] = keysym
;
239 this._sendKeyEvent(keysym
, code
, true);
241 _handleKeyPressTimeout: function (e
) {
242 // Did someone manage to sort out the key already?
243 if (this._pendingKey
=== null) {
249 code
= this._pendingKey
;
250 this._pendingKey
= null;
252 // We have no way of knowing the proper keysym with the
253 // information given, but the following are true for most
255 if ((e
.keyCode
>= 0x30) && (e
.keyCode
<= 0x39)) {
258 } else if ((e
.keyCode
>= 0x41) && (e
.keyCode
<= 0x5a)) {
260 var char = String
.fromCharCode(e
.keyCode
);
261 // A feeble attempt at the correct case
263 char = char.toUpperCase();
265 char = char.toLowerCase();
266 keysym
= char.charCodeAt();
272 this._keyDownList
[code
] = keysym
;
274 this._sendKeyEvent(keysym
, code
, true);
277 _handleKeyUp: function (e
) {
280 var code
= this._getKeyCode(e
);
282 // See comment in _handleKeyDown()
283 if (isMac() && (code
=== 'CapsLock')) {
284 this._sendKeyEvent(KeyTable
.XK_Caps_Lock
, 'CapsLock', true);
285 this._sendKeyEvent(KeyTable
.XK_Caps_Lock
, 'CapsLock', false);
289 // Do we really think this key is down?
290 if (!(code
in this._keyDownList
)) {
294 this._sendKeyEvent(this._keyDownList
[code
], code
, false);
296 delete this._keyDownList
[code
];
299 _allKeysUp: function () {
300 Log
.Debug(">> Keyboard.allKeysUp");
301 for (var code
in this._keyDownList
) {
302 this._sendKeyEvent(this._keyDownList
[code
], code
, false);
304 this._keyDownList
= {};
305 Log
.Debug("<< Keyboard.allKeysUp");
311 //Log.Debug(">> Keyboard.grab");
312 var c
= this._target
;
314 c
.addEventListener('keydown', this._eventHandlers
.keydown
);
315 c
.addEventListener('keyup', this._eventHandlers
.keyup
);
316 c
.addEventListener('keypress', this._eventHandlers
.keypress
);
318 // Release (key up) if window loses focus
319 window
.addEventListener('blur', this._eventHandlers
.blur
);
321 //Log.Debug("<< Keyboard.grab");
324 ungrab: function () {
325 //Log.Debug(">> Keyboard.ungrab");
326 var c
= this._target
;
328 c
.removeEventListener('keydown', this._eventHandlers
.keydown
);
329 c
.removeEventListener('keyup', this._eventHandlers
.keyup
);
330 c
.removeEventListener('keypress', this._eventHandlers
.keypress
);
331 window
.removeEventListener('blur', this._eventHandlers
.blur
);
333 // Release (key up) all keys that are in a down state
336 //Log.Debug(">> Keyboard.ungrab");
340 make_properties(Keyboard
, [
341 ['target', 'wo', 'dom'], // DOM element that captures keyboard input
343 ['onKeyEvent', 'rw', 'func'] // Handler for key press/release