Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions src/frontend/GuacamoleDisplay.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ import { createRdpReadyGate } from "./guacamole-ready.ts";
import {
remoteKeysym,
isToggleKey,
systemKeyLock,
watchRightAlt,
} from "./guacamole-meta-key.ts";

Expand Down Expand Up @@ -191,6 +192,7 @@ export const GuacamoleDisplay = forwardRef<
const unbindAudioRef = useRef<(() => void) | null>(null);
const unbindPointerRef = useRef<(() => void) | null>(null);
const unwatchAltRef = useRef<(() => void) | null>(null);
const systemKeysRef = useRef(systemKeyLock());
const scaleRef = useRef<number>(1);
const fitScaleRef = useRef<number>(1);
const zoomRef = useRef<number>(1);
Expand Down Expand Up @@ -233,6 +235,7 @@ export const GuacamoleDisplay = forwardRef<
unbindAudioRef.current = null;
unwatchAltRef.current?.();
unwatchAltRef.current = null;
systemKeysRef.current(false);
const client = clientRef.current;
clientRef.current = null;
tunnelWatchRef.current?.stop();
Expand Down Expand Up @@ -447,6 +450,7 @@ export const GuacamoleDisplay = forwardRef<
windowFocusedRef.current &&
(hasKeyboardFocusRef.current || displayIsFocused);

systemKeysRef.current(shouldCaptureInput);
if (!shouldCaptureInput) {
keyboard.reset();
keyboard.onkeydown = null;
Expand Down
22 changes: 22 additions & 0 deletions src/frontend/guacamole-meta-key.ts
Original file line number Diff line number Diff line change
Expand Up @@ -222,3 +222,25 @@ export function remoteKeysym(
optionKeysSent.delete(keysym);
return sent;
}

type KeyboardLock = {
lock?: (codes: string[]) => Promise<void>;
unlock?: () => void;
};

/**
* Claims the Windows keys for a focused session, so Win+R and the like reach
* the remote desktop instead of the local one. Browsers only honour the lock
* in fullscreen, so windowed sessions keep the toolbar keys.
*/
export function systemKeyLock(
keyboard = (navigator as Navigator & { keyboard?: KeyboardLock }).keyboard,
): (on: boolean) => void {
let locked = false;
return (on) => {
if (on === locked) return;
locked = on;
if (on) void keyboard?.lock?.(["MetaLeft", "MetaRight"])?.catch(() => {});
else keyboard?.unlock?.();
};
}
7 changes: 6 additions & 1 deletion tests/frontend/guacamole-key-release.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ if (!callback) throw new Error("Missing refreshKeyboardHandlers callback");
const runRefresh = new Function(
"state",
ts.transpileModule(
`const { keyboardRef, clientRef, displayElementRef, isVisible, windowFocusedRef, hasKeyboardFocusRef, sessionProtocolRef, remoteKeysym, isToggleKey } = state;
`const { keyboardRef, clientRef, displayElementRef, isVisible, windowFocusedRef, hasKeyboardFocusRef, sessionProtocolRef, remoteKeysym, isToggleKey, systemKeysRef } = state;
(${callback})();`,
{ compilerOptions: { target: ts.ScriptTarget.ES2022 } },
).outputText,
Expand Down Expand Up @@ -68,6 +68,7 @@ describe("remote keyboard focus loss", () => {
windowFocusedRef: { current: true },
hasKeyboardFocusRef: { current: true },
sessionProtocolRef: { current: "vnc" },
systemKeysRef: { current: vi.fn() },
remoteKeysym,
isToggleKey,
};
Expand All @@ -91,6 +92,8 @@ describe("remote keyboard focus loss", () => {
[...keys].sort((a, b) => a - b).map((key) => [0, key]),
);
expect(Object.keys(keyboard.pressed)).toEqual([]);
// The Windows keys go back to the local system with the focus.
expect(state.systemKeysRef.current.mock.calls).toEqual([[true], [false]]);
expect(keyboard.onkeydown).toBeNull();
expect(keyboard.onkeyup).toBeNull();
keyboard.press(0x6c);
Expand Down Expand Up @@ -137,6 +140,7 @@ describe("remote keyboard on a Mac", () => {
windowFocusedRef: { current: true },
hasKeyboardFocusRef: { current: true },
sessionProtocolRef: { current: protocol },
systemKeysRef: { current: vi.fn() },
remoteKeysym,
isToggleKey,
});
Expand Down Expand Up @@ -182,6 +186,7 @@ describe("Korean toggle keys with the real keyboard", () => {
windowFocusedRef: { current: true },
hasKeyboardFocusRef: { current: true },
sessionProtocolRef: { current: "rdp" },
systemKeysRef: { current: vi.fn() },
remoteKeysym,
isToggleKey,
};
Expand Down
34 changes: 33 additions & 1 deletion tests/frontend/guacamole-meta-key.test.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import { describe, expect, it } from "vitest";
import { describe, expect, it, vi } from "vitest";
import {
detectMetaKeyFamily,
metaKeyLabels,
remoteKeysym,
systemKeyLock,
watchRightAlt,
} from "../../src/frontend/guacamole-meta-key.js";

Expand Down Expand Up @@ -179,3 +180,34 @@ describe("Option on an ARD host", () => {
stop();
});
});

describe("systemKeyLock", () => {
it("claims the Windows keys once and gives them back once", () => {
const keyboard = {
lock: vi.fn(() => Promise.resolve()),
unlock: vi.fn(),
};
const set = systemKeyLock(keyboard);
set(true);
set(true);
expect(keyboard.lock).toHaveBeenCalledTimes(1);
expect(keyboard.lock).toHaveBeenCalledWith(["MetaLeft", "MetaRight"]);
set(false);
set(false);
expect(keyboard.unlock).toHaveBeenCalledTimes(1);
});

it("is harmless where the browser has no keyboard lock", () => {
const set = systemKeyLock(undefined);
expect(() => {
set(true);
set(false);
}).not.toThrow();
});

it("ignores a refused lock", async () => {
const set = systemKeyLock({ lock: () => Promise.reject(new Error("no")) });
set(true);
await Promise.resolve();
});
});