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

@ -10,11 +10,12 @@
// (예약 정책 — 플랫폼 · 기능 게이트 — 은 keybinding-accelerator-policy.ts 의 순수 함수)
import { EventEmitter } from 'events'
import { globalShortcut } from 'electron'
import { globalShortcut, powerMonitor } from 'electron'
import { uIOhook, UiohookKey } from 'uiohook-napi'
import type { UiohookKeyboardEvent, UiohookMouseEvent } from 'uiohook-napi'
import { getLogger } from './LoggerService'
import { acquireGlobalInputHook } from './global-input-hook'
import { forgetHeldModifiers } from './modifier-state'
import { configGet, loadKeyBindings, onConfigChanged } from './ConfigService'
import { acceleratorsToReserve, suggestionCaptureAccelerators } from './keybinding-accelerator-policy'
import type { ReservableBinding } from './keybinding-accelerator-policy'
@ -284,6 +285,7 @@ class KeyBindingService extends EventEmitter {
/** 기능 스위치(제안 on/off) 변경 시 accelerator 를 다시 맞추는 구독 해제자 */
private _offConfigChanged: (() => void) | null = null
private _offSessionLock: (() => void) | null = null
private _onKeyDown: ((e: UiohookKeyboardEvent) => void) | null = null
private _onKeyUp: ((e: UiohookKeyboardEvent) => void) | null = null
@ -337,6 +339,7 @@ class KeyBindingService extends EventEmitter {
if (event.key === 'suggestionEnabled') this._syncAccelerators()
})
this._syncAccelerators()
this._watchSessionLock()
logger.info('Key binding listeners attached to global input hook')
} catch (error) {
const d3roError = new D3ROError(
@ -378,6 +381,8 @@ class KeyBindingService extends EventEmitter {
this._offConfigChanged = null
this._clearRuntimeState()
this._unregisterOwnedAccelerators()
this._offSessionLock?.()
this._offSessionLock = null
logger.info('uiohook stopped')
} catch (error) {
@ -540,6 +545,23 @@ class KeyBindingService extends EventEmitter {
this._ownedAccelerators.clear()
}
/**
* 화면 잠금·절전 중에는 잠금 데스크톱이 keyup 을 가져가 훅이 받지 못한다.
* 그대로 두면 hold 녹음이 안 끝나고, 잠금을 푼 뒤 첫 단축키가 auto-repeat 로 버려진다.
*/
private _watchSessionLock(): void {
const releaseHeld = (): void => {
this._machine.releaseAll()
this._altGr.reset()
forgetHeldModifiers()
}
const events = ['lock-screen', 'suspend'] as const
for (const event of events) powerMonitor.on(event, releaseHeld)
this._offSessionLock = () => {
for (const event of events) powerMonitor.removeListener(event, releaseHeld)
}
}
/** 눌림 · 더블프레스 · AltGr 보류 타이머를 모두 비운다. */
private _clearRuntimeState(): void {
this._machine.reset()

View file

@ -28,6 +28,11 @@ function attach(): void {
})
}
/** 떼는 이벤트를 놓쳤을 때(Win+L 의 Win 등) 눌림 상태를 잊는다. 안 그러면 다음 삽입이 매번 시간 초과까지 기다린다. */
export function forgetHeldModifiers(): void {
held.clear()
}
/** 추적을 시작한다 — 단축키가 눌리기 전에 붙어 있어야 누른 상태를 안다. */
export function trackModifierKeys(): void {
attach()