diff --git a/src/backend/engine/worker/rdp/client.ts b/src/backend/engine/worker/rdp/client.ts index 859d956..7ad0e8f 100644 --- a/src/backend/engine/worker/rdp/client.ts +++ b/src/backend/engine/worker/rdp/client.ts @@ -5,7 +5,7 @@ import { Status } from "../../protocol.js"; import type { RdpClient as WasmRdpClient } from "../../wasm/engine.js"; import type { PixelSource } from "../display.js"; import type { RemoteClient, RemoteHost, Settings } from "../remote.js"; -import { Keyboard } from "./keyboard.js"; +import { Keyboard, type Locks } from "./keyboard.js"; import { DEFAULT_LAYOUT } from "./layouts.js"; import { DriveCallbacks } from "./drive.js"; import { kerberosRequest } from "./kdc.js"; @@ -540,7 +540,7 @@ export class RdpClient implements RemoteClient, PixelSource { this.height = event.h; display.setSource(this); display.resize(event.w, event.h); - this.rdp?.sync_locks(false, false, false); + this.syncLocks(this.keyboard.locks); this.host.ready(); break; case "resize": @@ -631,12 +631,17 @@ export class RdpClient implements RemoteClient, PixelSource { private sendKey(keysym: number, pressed: boolean): void { for (const out of this.keyboard.key(keysym, pressed)) { - if ("scancode" in out) this.rdp?.key(out.scancode, out.down); + if ("locks" in out) this.syncLocks(out.locks); + else if ("scancode" in out) this.rdp?.key(out.scancode, out.down); else this.rdp?.unicode(out.unicode, out.down); } this.pump(); } + private syncLocks(locks: Locks): void { + this.rdp?.sync_locks(false, locks.num, locks.caps); + } + resize(width: number, height: number, askedDpi: number): void { const next = nextSize(this.size, width, height, askedDpi); if (!next) return; diff --git a/src/backend/engine/worker/rdp/keyboard.ts b/src/backend/engine/worker/rdp/keyboard.ts index c1fdb08..b65ae95 100644 --- a/src/backend/engine/worker/rdp/keyboard.ts +++ b/src/backend/engine/worker/rdp/keyboard.ts @@ -93,6 +93,18 @@ const ALT_L = 0x38; const WIN_L = EXT | 0x5b; const WIN_R = EXT | 0x5c; const CAPS = 0x3a; +const NUM_LOCK = 0x45; + +/** + * The Num Lock state a keypad key needs on the server: digits and the + * decimal point only type with it on, the navigation keys only with it off. + */ +function keypadNumLock(keysym: number): boolean | undefined { + const digit = keysym >= 0xffb0 && keysym <= 0xffb9; + if (digit || keysym === 0xffae || keysym === 0xffac) return true; + if (keysym >= 0xff95 && keysym <= 0xff9f) return false; + return undefined; +} type Layer = "base" | "shift" | "altgr" | "altgrShift"; @@ -138,7 +150,15 @@ function indexLayout(spec: LayoutSpec): Map { } export type KeyOutput = - { scancode: number; down: boolean } | { unicode: number; down: boolean }; + | { scancode: number; down: boolean } + | { unicode: number; down: boolean } + /** Sets the server's lock keys, sent before a key that needs them. */ + | { locks: Locks }; + +export interface Locks { + num: boolean; + caps: boolean; +} /** * Turns the keysyms a Guacamole client sends into RDP scancodes for the @@ -154,6 +174,7 @@ export class Keyboard { /** What each held keysym pressed, so its release lifts the same thing. */ private readonly pressedBy = new Map(); private capsLock = false; + private numLock = false; constructor(layout: string) { this.spec = LAYOUTS[layout] ?? LAYOUTS[DEFAULT_LAYOUT]; @@ -164,6 +185,11 @@ export class Keyboard { return this.spec.id; } + /** The lock state the server should have, to sync on (re)connect. */ + get locks(): Locks { + return { num: this.numLock, caps: this.capsLock }; + } + key(keysym: number, pressed: boolean): KeyOutput[] { if (!pressed) return this.release(keysym); // Key repeat: a second press without a release. @@ -195,8 +221,9 @@ export class Keyboard { const fixed = FIXED[keysym]; if (fixed !== undefined) { if (fixed === CAPS) this.capsLock = !this.capsLock; + if (fixed === NUM_LOCK) this.numLock = !this.numLock; this.pressedBy.set(keysym, [{ scancode: fixed, down: false }]); - return [this.downKey(fixed)]; + return [...this.syncNumLock(keysym), this.downKey(fixed)]; } const char = keysymToChar(keysym); if (char === null) return []; @@ -216,6 +243,17 @@ export class Keyboard { return this.unicode(char); } + /** + * The server reads a keypad scancode by its own Num Lock, not the user's, + * so set it to what the keysym means before sending the key. + */ + private syncNumLock(keysym: number): KeyOutput[] { + const want = keypadNumLock(keysym); + if (want === undefined || want === this.numLock) return []; + this.numLock = want; + return [{ locks: this.locks }]; + } + private release(keysym: number): KeyOutput[] { const held = this.pressedBy.get(keysym); if (!held) return []; diff --git a/tests/backend/engine/worker/rdp/keyboard.test.ts b/tests/backend/engine/worker/rdp/keyboard.test.ts index 8ba7605..20c87d0 100644 --- a/tests/backend/engine/worker/rdp/keyboard.test.ts +++ b/tests/backend/engine/worker/rdp/keyboard.test.ts @@ -150,3 +150,39 @@ describe("Korean input keys", () => { expect(tap(kb, 0xff34)).toEqual([down(0x71), up(0x71)]); }); }); + +describe("Keyboard keypad Num Lock", () => { + const KP_1 = 0xffb1; + const KP_END = 0xff9c; + const NUM_LOCK = 0xff7f; + + it("turns the server's Num Lock on before a keypad digit", () => { + const kb = new Keyboard("en-us-qwerty"); + expect(tap(kb, KP_1)).toEqual([ + { locks: { num: true, caps: false } }, + down(0x4f), + up(0x4f), + ]); + // Already on: the next digit goes straight through. + expect(tap(kb, KP_1)).toEqual([down(0x4f), up(0x4f)]); + expect(kb.locks).toEqual({ num: true, caps: false }); + }); + + it("turns it off for keypad navigation, keeping Caps Lock", () => { + const kb = new Keyboard("en-us-qwerty"); + tap(kb, 0xffe5); + tap(kb, KP_1); + expect(tap(kb, KP_END)).toEqual([ + { locks: { num: false, caps: true } }, + down(0x4f), + up(0x4f), + ]); + }); + + it("follows Num Lock pressed in the session", () => { + const kb = new Keyboard("en-us-qwerty"); + expect(tap(kb, NUM_LOCK)).toEqual([down(0x45), up(0x45)]); + expect(kb.locks.num).toBe(true); + expect(tap(kb, KP_1)).toEqual([down(0x4f), up(0x4f)]); + }); +});