fix(keybinding): release held shortcuts and modifiers when the screen locks or the PC sleeps

The lock desktop swallows keyup, so a hold-to-talk recording never ended,
the first shortcut after unlocking was dropped as auto-repeat, and Win from
Win+L made the next suggestion accept wait out the full 1.5s modifier timeout.
On powerMonitor lock-screen/suspend the chord machine now emits released for
emitted triggers and clears its state, and modifier tracking forgets held keys.
This commit is contained in:
Yun Chan 2026-10-21 00:28:40 +09:00
parent 8d003c8fca
commit 3e6cfd1ba8
6 changed files with 94 additions and 2 deletions

View file

@ -5,7 +5,7 @@
// 3) 사용자가 비운 액션([])은 다른 액션 저장 · 재시작 뒤에도 비어 있다.
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'
import { globalShortcut, ipcMain } from 'electron'
import { globalShortcut, ipcMain, powerMonitor } from 'electron'
import { uIOhook } from 'uiohook-napi'
import type { UiohookMouseEvent } from 'uiohook-napi'
import { IPC_CHANNELS } from '@d3ro/core/ipc-channels'
@ -220,6 +220,40 @@ describe('KeyBindingService — globalShortcut 예약 · 마우스 release', ()
expect(events).toEqual(['dictation:pressed', 'dictation:released'])
})
it('화면이 잠기면 누르고 있던 hold 바인딩에 released 를 보내고, 잠금을 푼 뒤 첫 누름이 다시 동작한다', () => {
setPlatform('win32')
const map = createDefaultBindingMap()
map.dictation = [{ device: 'mouse', code: MouseButton.Back, ctrl: false, alt: false, shift: false, meta: false }]
configSet('keyBindings', map)
vi.mocked(powerMonitor.on).mockClear()
const service = getKeyBindingService()
service.loadFromConfig()
service.start()
const handlerOf = (event: string): ((e: UiohookMouseEvent) => void) => {
const call = vi.mocked(uIOhook.on).mock.calls.find((c) => c[0] === event)
if (!call) throw new Error(`no ${event} handler`)
return call[1] as (e: UiohookMouseEvent) => void
}
const lockCall = vi.mocked(powerMonitor.on).mock.calls.find((c) => c[0] === 'lock-screen')
if (!lockCall) throw new Error('no lock-screen handler')
const onLock = lockCall[1] as () => void
const events: string[] = []
const listener = (p: KeyBindingTriggerPayload): void => {
events.push(`${p.actionId}:${p.type}`)
}
service.on('triggered', listener)
const back = { button: MouseButton.Back, altKey: false, ctrlKey: false, shiftKey: false, metaKey: false } as unknown as UiohookMouseEvent
handlerOf('mousedown')(back)
onLock() // 떼는 이벤트는 잠금 데스크톱이 가져간다
handlerOf('mousedown')(back)
service.off('triggered', listener)
expect(events).toEqual(['dictation:pressed', 'dictation:released', 'dictation:pressed'])
})
})
// ============================================================

View file

@ -73,6 +73,10 @@ vi.mock('electron', () => ({
shell: {
openExternal: vi.fn(async () => undefined),
},
powerMonitor: {
on: vi.fn(),
removeListener: vi.fn(),
},
safeStorage: {
isEncryptionAvailable: vi.fn(() => false),
encryptString: vi.fn((s: string) => Buffer.from(s, 'utf-8')),