- commit
- 8d859d2b2c552a39bfd885166e7b1f384f4c16e2
- parent
- 10aa184f10ed2ffe7513c3bdad947d2a15291c18
- author
- Dami <dami.etoile@gmail.com>
| A | portal/ | 14 | +++++++++++ |
| A | portal/ | 38 | ++++++++++++++++++++++++++++++ |
| M | portal/ | 16 | +------------ |
@@ -0,0 +1,14 @@
1+export function mapKey(event) {2+ const key = event.key;3+ const map = {4+ 'ArrowUp': 'Up', 'ArrowDown': 'Down', 'ArrowLeft': 'Left', 'ArrowRight': 'Right',5+ ' ': 'Space', 'Delete': 'Delete', 'Backspace': 'Backspace',6+ 'Enter': 'Enter', 'Tab': 'Tab', 'Escape': 'Escape',7+ 'Shift': 'Shift', 'Control': 'Control', 'Alt': 'Alt', 'Meta': 'Meta',8+ 'Home': 'Home', 'End': 'End', 'PageUp': 'PageUp', 'PageDown': 'PageDown',9+ };10+ const asciiPrintable = key.length === 111+ && key.codePointAt(0) >= 0x2012+ && key.codePointAt(0) <= 0x7e;13+ return map[key] || (asciiPrintable ? key : null);14+}@@ -0,0 +1,38 @@
1+import assert from 'node:assert/strict';2+import test from 'node:test';3+4+import { mapKey } from './keyboard-map.mjs';5+6+test('maps the current named keyboard controls exactly', () => {7+ for (const [key, expected] of [8+ ['ArrowUp', 'Up'],9+ ['ArrowDown', 'Down'],10+ ['ArrowLeft', 'Left'],11+ ['ArrowRight', 'Right'],12+ [' ', 'Space'],13+ ['Delete', 'Delete'],14+ ['Backspace', 'Backspace'],15+ ['Enter', 'Enter'],16+ ['Tab', 'Tab'],17+ ['Escape', 'Escape'],18+ ['Shift', 'Shift'],19+ ['Control', 'Control'],20+ ['Alt', 'Alt'],21+ ['Meta', 'Meta'],22+ ['Home', 'Home'],23+ ['End', 'End'],24+ ['PageUp', 'PageUp'],25+ ['PageDown', 'PageDown'],26+ ]) {27+ assert.equal(mapKey({ key }), expected, key);28+ }29+});30+31+test('preserves only single ASCII-printable keys', () => {32+ for (const key of ['!', 'a', 'A', '0', '~']) {33+ assert.equal(mapKey({ key }), key, key);34+ }35+ for (const key of ['F1', 'F12', 'é', '😀', 'CapsLock', 'Insert', '']) {36+ assert.equal(mapKey({ key }), null, key);37+ }38+});@@ -107,6 +107,7 @@ import {
107107 normalizeInvitationSummary,108108 shortPeerFingerprint,109109 } from './enrollment.mjs';110+import { mapKey } from './keyboard-map.mjs';110111 111112 let controlMode = false;112113 let connected = false;@@ -2317,21 +2318,6 @@ function scaleCoords(clientX, clientY) {
23172318 }) ?? { x: 0, y: 0 };23182319 }23192320 2320-function mapKey(e) {2321- const key = e.key;2322- const map = {2323- 'ArrowUp': 'Up', 'ArrowDown': 'Down', 'ArrowLeft': 'Left', 'ArrowRight': 'Right',2324- ' ': 'Space', 'Delete': 'Delete', 'Backspace': 'Backspace',2325- 'Enter': 'Enter', 'Tab': 'Tab', 'Escape': 'Escape',2326- 'Shift': 'Shift', 'Control': 'Control', 'Alt': 'Alt', 'Meta': 'Meta',2327- 'Home': 'Home', 'End': 'End', 'PageUp': 'PageUp', 'PageDown': 'PageDown',2328- };2329- const asciiPrintable = key.length === 12330- && key.codePointAt(0) >= 0x202331- && key.codePointAt(0) <= 0x7e;2332- return map[key] || (asciiPrintable ? key : null);2333-}2334-23352321 const RELIABLE_INPUT_QUEUE_LIMIT = 128;23362322 const IN_FLIGHT_INPUT_RESTORE_RESERVE = 1;23372323 const RELIABLE_INPUT_ENQUEUE_LIMIT = RELIABLE_INPUT_QUEUE_LIMIT