diff --git a/src/hotkey.ts b/src/hotkey.ts index c5cc9b1..be8bb51 100644 --- a/src/hotkey.ts +++ b/src/hotkey.ts @@ -80,7 +80,7 @@ export function registerHotkey( const once = (options as HotkeyEventListenerOptions)?.once; const native_options = options && typeof options === "object" - ? { capture: options.capture, passive: options.passive } + ? { capture: options.capture, passive: options.passive, signal: options.signal } : options; const unregister = listen(target, eventName, function (this: EventTarget, e: KeyboardEvent) { diff --git a/tests/hotkey.spec.js b/tests/hotkey.spec.js index 2a60d06..91b21b2 100644 --- a/tests/hotkey.spec.js +++ b/tests/hotkey.spec.js @@ -429,3 +429,41 @@ test("should trigger when Alt+0 is pressed", async ({ page }) => { const triggered = await page.evaluate(() => window.hotkeyTriggered); expect(triggered).toBe(true); }); + +test("should not trigger after AbortSignal is aborted", async ({ page }) => { + await page.evaluate(() => { + window.hotkeyCount = 0; + window.controller = new AbortController(); + + window.registerHotkey(document, "Ctrl+K", () => { + window.hotkeyCount++; + }, "keydown", { signal: window.controller.signal }); + }); + + await page.keyboard.press("Control+k"); + + await page.evaluate(() => window.controller.abort()); + + await page.keyboard.press("Control+k"); + + const count = await page.evaluate(() => window.hotkeyCount); + expect(count).toBe(1); +}); + +test("should not trigger when AbortSignal is already aborted", async ({ page }) => { + await page.evaluate(() => { + window.hotkeyCount = 0; + + const controller = new AbortController(); + controller.abort(); + + window.registerHotkey(document, "Ctrl+K", () => { + window.hotkeyCount++; + }, "keydown", { signal: controller.signal }); + }); + + await page.keyboard.press("Control+k"); + + const count = await page.evaluate(() => window.hotkeyCount); + expect(count).toBe(0); +});