diff --git a/src/component/utils/keyboards/guacamole.ts b/src/component/utils/keyboards/guacamole.ts index 816b1bdf..4cf0c8b1 100644 --- a/src/component/utils/keyboards/guacamole.ts +++ b/src/component/utils/keyboards/guacamole.ts @@ -1,73 +1,8 @@ // https://github.com/apache/guacamole-client/blob/1ca1161a68030565a37319ec6275556dfcd1a1af/guacamole-common-js/src/main/webapp/modules/Keyboard.js -import GuacamoleKeyboard from './guacamole.js' +import GuacamoleKeyboard from './guacamole/keyboard' +import type { Interface } from './guacamole/keyboard' -export interface GuacamoleKeyboardInterface { - /** - * Fired whenever the user presses a key with the element associated - * with this Guacamole.Keyboard in focus. - * - * @event - * @param {Number} keysym The keysym of the key being pressed. - * @return {Boolean} true if the key event should be allowed through to the - * browser, false otherwise. - */ - onkeydown?: (keysym: number) => boolean - - /** - * Fired whenever the user releases a key with the element associated - * with this Guacamole.Keyboard in focus. - * - * @event - * @param {Number} keysym The keysym of the key being released. - */ - onkeyup?: (keysym: number) => void - - /** - * Marks a key as pressed, firing the keydown event if registered. Key - * repeat for the pressed key will start after a delay if that key is - * not a modifier. The return value of this function depends on the - * return value of the keydown event handler, if any. - * - * @param {Number} keysym The keysym of the key to press. - * @return {Boolean} true if event should NOT be canceled, false otherwise. - */ - press: (keysym: number) => boolean - - /** - * Marks a key as released, firing the keyup event if registered. - * - * @param {Number} keysym The keysym of the key to release. - */ - release: (keysym: number) => void - - /** - * Presses and releases the keys necessary to type the given string of - * text. - * - * @param {String} str - * The string to type. - */ - type: (str: string) => void - - /** - * Resets the state of this keyboard, releasing all keys, and firing keyup - * events for each released key. - */ - reset: () => void - - /** - * Attaches event listeners to the given Element, automatically translating - * received key, input, and composition events into simple keydown/keyup - * events signalled through this Guacamole.Keyboard's onkeydown and - * onkeyup handlers. - * - * @param {Element|Document} element - * The Element to attach event listeners to for the sake of handling - * key or input events. - */ - listenTo: (element: Element | Document) => void - - // custom functions +export interface GuacamoleKeyboardInterface extends Interface { removeListener: () => void } diff --git a/src/component/utils/keyboards/guacamole/keyboard.d.ts b/src/component/utils/keyboards/guacamole/keyboard.d.ts new file mode 100644 index 00000000..6078eb8c --- /dev/null +++ b/src/component/utils/keyboards/guacamole/keyboard.d.ts @@ -0,0 +1,68 @@ +declare export interface Interface { + /** + * Fired whenever the user presses a key with the element associated + * with this Guacamole.Keyboard in focus. + * + * @event + * @param {Number} keysym The keysym of the key being pressed. + * @return {Boolean} true if the key event should be allowed through to the + * browser, false otherwise. + */ + onkeydown?: (keysym: number) => boolean + + /** + * Fired whenever the user releases a key with the element associated + * with this Guacamole.Keyboard in focus. + * + * @event + * @param {Number} keysym The keysym of the key being released. + */ + onkeyup?: (keysym: number) => void + + /** + * Marks a key as pressed, firing the keydown event if registered. Key + * repeat for the pressed key will start after a delay if that key is + * not a modifier. The return value of this function depends on the + * return value of the keydown event handler, if any. + * + * @param {Number} keysym The keysym of the key to press. + * @return {Boolean} true if event should NOT be canceled, false otherwise. + */ + press: (keysym: number) => boolean + + /** + * Marks a key as released, firing the keyup event if registered. + * + * @param {Number} keysym The keysym of the key to release. + */ + release: (keysym: number) => void + + /** + * Presses and releases the keys necessary to type the given string of + * text. + * + * @param {String} str + * The string to type. + */ + type: (str: string) => void + + /** + * Resets the state of this keyboard, releasing all keys, and firing keyup + * events for each released key. + */ + reset: () => void + + /** + * Attaches event listeners to the given Element, automatically translating + * received key, input, and composition events into simple keydown/keyup + * events signalled through this Guacamole.Keyboard's onkeydown and + * onkeyup handlers. + * + * @param {Element|Document} element + * The Element to attach event listeners to for the sake of handling + * key or input events. + */ + listenTo: (element: Element | Document) => void +} + +declare export default function (element?: Element): Interface diff --git a/src/component/utils/keyboards/guacamole.js b/src/component/utils/keyboards/guacamole/keyboard.js similarity index 100% rename from src/component/utils/keyboards/guacamole.js rename to src/component/utils/keyboards/guacamole/keyboard.js diff --git a/src/component/utils/keyboards/novnc.js b/src/component/utils/keyboards/novnc.js deleted file mode 100644 index b8b7d5c1..00000000 --- a/src/component/utils/keyboards/novnc.js +++ /dev/null @@ -1,2 +0,0 @@ -import Keyboard from './novnc/keyboard.js' -export default Keyboard diff --git a/src/component/utils/keyboards/novnc.ts b/src/component/utils/keyboards/novnc.ts index a7966b6e..de3fa91f 100644 --- a/src/component/utils/keyboards/novnc.ts +++ b/src/component/utils/keyboards/novnc.ts @@ -1,13 +1,7 @@ // https://github.com/novnc/noVNC/blob/ca6527c1bf7131adccfdcc5028964a1e67f9018c/core/input/keyboard.js -import Keyboard from './novnc.js' +import Keyboard from './novnc/keyboard' -export interface NoVncKeyboardInterface { - // original functions - onkeyevent: (keysym: number | null, code: string, down: boolean) => boolean - grab: () => void - ungrab: () => void - - // custom functions +export interface NoVncKeyboardInterface extends Keyboard { onkeydown?: (keysym: number) => boolean onkeyup?: (keysym: number) => void release: (keysym: number) => void @@ -16,14 +10,14 @@ export interface NoVncKeyboardInterface { } export default function (element?: Element): NoVncKeyboardInterface { - // @ts-ignore - const keyboard = new Keyboard(element) + const keyboard = new Keyboard(element) as NoVncKeyboardInterface // map on key event to onkeydown and onkeyup keyboard.onkeyevent = function (keysym: number | null, code: string, down: boolean) { if (keysym === null) return false - if (down) return this.onkeydown(keysym, code) - this.onkeyup(keysym, code) + if (down && this.onkeydown) return this.onkeydown(keysym) + if (!down && this.onkeyup) this.onkeyup(keysym) + return false } // add release function diff --git a/src/component/utils/keyboards/novnc/keyboard.d.ts b/src/component/utils/keyboards/novnc/keyboard.d.ts new file mode 100644 index 00000000..24f4a3fa --- /dev/null +++ b/src/component/utils/keyboards/novnc/keyboard.d.ts @@ -0,0 +1,23 @@ +declare export default class Keyboard { + constructor (element?: Element) + + _target: Element | Document | null + _keyDownList: { [key: string]: number } + _altGrArmed: boolean + _eventHandlers: { + keyup: (event: KeyboardEvent) => void + keydown: (event: KeyboardEvent) => void + blur: () => void + } + + _sendKeyEvent(keysym: number, code: string, down: boolean): void + _getKeyCode(e: KeyboardEvent): string + _handleKeyDown(e: KeyboardEvent): void + _handleKeyUp(e: KeyboardEvent): void + _handleAltGrTimeout(): void + _allKeysUp(): void + + onkeyevent: (keysym: number | null, code: string, down: boolean) => boolean + grab: () => void + ungrab: () => void +}