fix(keybinding): stop stealing chords on Windows, release mouse holds, keep cleared actions

This commit is contained in:
Yun Chan 2026-09-28 02:16:19 +09:00
parent 115e8488ac
commit 1a4c39cb94
8 changed files with 927 additions and 148 deletions

View file

@ -10,14 +10,14 @@
// 어댑터(데스크톱 KeyBindingService)의 책임:
// - 장치 이벤트 → bindingKey / VK 변환
// - 관측 불가능한 키 걸러내기(등록 전에)
// - macOS beep 차단용 accelerator, 설정 로딩
// - macOS beep 차단용 accelerator(플랫폼 · 기능 게이트 포함), 설정 로딩
//
// 런타임 상태는 액션 id 가 아니라 bindingKey() 로 키잉한다 —
// 한 액션에 여러 바인딩이 붙고, 여러 액션이 한 바인딩을 공유하기 때문이다.
import { TIMING } from './constants'
import { bindingKey, isModifierKeyCode, normalizeBinding } from './keybinding'
import type { KeyBinding, KeyBindingActionId } from './keybinding'
import type { BindingDevice, KeyBinding, KeyBindingActionId } from './keybinding'
// ============================================================
// 공개 타입 · 포트
@ -46,6 +46,15 @@ export interface ChordBindingEntry {
binding: KeyBinding
}
/**
* 놓인 물리 키/버튼. keyUp 이 정확 매칭에 실패했을 때 "눌림 상태 바인딩의 구성 요소가
* 놓였는가" 를 판정하는 데 쓴다. 키보드는 Windows VK, 마우스는 MouseButton 코드다.
*/
export interface ChordReleasedComponent {
device: BindingDevice
code: number
}
/**
* 시간 포트. 실제 구현은 Date.now / setTimeout, 테스트는 가짜 타이머를 넣는다.
* schedule 은 취소 함수를 돌려준다 — 타이머 핸들 타입을 core 가 알 필요가 없다.
@ -118,16 +127,33 @@ export function isAltGrShapedBinding(binding: KeyBinding): boolean {
}
/**
* 놓인 키(VK)가 바인딩의 주 키이거나 켜진 수정자의 구성 키인가.
* 마우스 바인딩(Ctrl+뒤로 등)도 수정자를 먼저 놓으면 release 되어야 한다 —
* 그 뒤의 mouseup 은 수정자 없이 들어와 정확 매칭이 실패하기 때문이다.
* 놓인 키/버튼이 바인딩의 주 키이거나 켜진 수정자의 구성 키인가.
*
* - 주 키: 장치와 코드가 같으면 수정자 플래그와 무관하게 구성 요소다. 마우스 X1 을 누른 채
* Alt 를 더 누르고 버튼을 놓으면 mouseup 이 `X1+Alt` 로 들어와 정확 매칭이 실패하는데,
* 이때도 X1 누름은 놓여야 한다(아니면 hold-to-talk 가 계속 녹음한다).
* - 수정자: 키보드 수정자 키만 해당한다. 마우스 바인딩(Ctrl+뒤로 등)도 수정자를 먼저 놓으면
* release 되어야 한다 — 그 뒤의 mouseup 은 수정자 없이 들어와 정확 매칭이 실패하기 때문이다.
*/
function isComponentOf(vk: number, binding: KeyBinding): boolean {
if (binding.device === 'keyboard' && binding.code === vk) return true
const flag = modifierFlagOfKeyCode(vk)
export function isChordComponentOf(
component: ChordReleasedComponent,
binding: KeyBinding
): boolean {
if (binding.device === component.device && binding.code === component.code) return true
if (component.device !== 'keyboard') return false
const flag = modifierFlagOfKeyCode(component.code)
return flag !== null && binding[flag]
}
/** keyUp 의 두 번째 인자 — 숫자는 키보드 VK(옛 호출 형태)다. */
function toReleasedComponent(
component: number | ChordReleasedComponent | null
): ChordReleasedComponent | null {
if (component === null) return null
if (typeof component === 'number') return { device: 'keyboard', code: component }
return component
}
// ============================================================
// 상태 머신
// ============================================================
@ -297,27 +323,35 @@ export class ChordStateMachine {
/**
* 키/버튼 놓임.
*
* eventKey 가 눌림 상태와 정확히 맞으면 그 바인딩을 놓는다. 아니면 componentVk(놓인
* 키의 VK)가 눌림 상태 바인딩의 주 키/수정자인지 본다 — Alt+1 에서 수정자를 먼저
* 놓아도 release 를 놓치지 않기 위해서다. mouseup 은 componentVk 를 null 로 넘겨
* 정확 매칭만 한다(마우스 버튼은 키보드 바인딩의 구성 키가 아니다).
* eventKey 가 눌림 상태와 정확히 맞으면 그 바인딩을 놓는다. 아니면 component(놓인
* 키/버튼)가 눌림 상태 바인딩의 주 키/수정자인지 본다 — Alt+1 에서 수정자를 먼저
* 놓거나, 마우스 버튼을 누른 채 수정자를 더 눌러도 release 를 놓치지 않기 위해서다.
*
* component 가 숫자면 키보드 VK 다(옛 호출 형태). null 이면 정확 매칭만 한다.
* 마우스 버튼은 물리적으로 하나이므로 그 버튼을 쓰는 눌림 바인딩을 모두 놓는다.
*/
keyUp(eventKey: string | null, componentVk: number | null): void {
keyUp(
eventKey: string | null,
component: number | ChordReleasedComponent | null
): void {
if (eventKey !== null && this._isDown.has(eventKey)) {
this._release(eventKey)
return
}
if (componentVk === null) return
const released = toReleasedComponent(component)
if (released === null) return
const matches: string[] = []
for (const [key, entries] of this._byKey) {
if (!this._isDown.has(key)) continue
const first = entries[0]
if (first === undefined) continue
if (isComponentOf(componentVk, first.binding)) {
this._release(key)
return
if (isChordComponentOf(released, first.binding)) {
matches.push(key)
if (released.device === 'keyboard') break
}
}
for (const key of matches) this._release(key)
}
// ── 내부 ───────────────────────────────────────────────