State.lastFired의 타입을 UInt8에서 CGKeyCode로 변경하고, tiltMask 계산 로직을 개선하여 마우스의 틸트 모드에 따른 키 코드 매핑을 명확히 함

This commit is contained in:
jsahn
2026-09-28 02:19:46 +09:00
parent f29845d981
commit 178ef4b2bd
+9 -5
View File
@@ -17,7 +17,7 @@ struct ReportSource: Hashable {
final class State {
var buffers: [IOHIDDevice: UnsafeMutablePointer<UInt8>] = [:]
var lastBits: [ReportSource: UInt8] = [:]
var lastFired: [UInt8: TimeInterval] = [:]
var lastFired: [CGKeyCode: TimeInterval] = [:]
var opened = false
var lastStatus = ""
}
@@ -37,7 +37,11 @@ func sendCommandKey(_ keyCode: CGKeyCode) {
up.post(tap: .cgSessionEventTap)
}
let tiltMask: UInt8 = 0x60
// The mouse reports tilt on either bit pair depending on its mode after (re)connecting:
// 0x20/0x40, or 0x08/0x10 (seen after a replug).
let leftTiltMask: UInt8 = 0x20 | 0x08
let rightTiltMask: UInt8 = 0x40 | 0x10
let tiltMask = leftTiltMask | rightTiltMask
// Ignore a second press of the same tilt within this window (switch bounce).
let debounceInterval: TimeInterval = 0.12
@@ -52,9 +56,9 @@ let reportCallback: IOHIDReportCallback = {
let pressed = value & ~(state.lastBits[source] ?? 0)
state.lastBits[source] = value
let now = ProcessInfo.processInfo.systemUptime
for (mask, keyCode) in [(UInt8(0x20), CGKeyCode(33)), (UInt8(0x40), CGKeyCode(30))] where pressed & mask != 0 {
if let last = state.lastFired[mask], now - last < debounceInterval { continue }
state.lastFired[mask] = now
for (mask, keyCode) in [(leftTiltMask, CGKeyCode(33)), (rightTiltMask, CGKeyCode(30))] where pressed & mask != 0 {
if let last = state.lastFired[keyCode], now - last < debounceInterval { continue }
state.lastFired[keyCode] = now
sendCommandKey(keyCode)
}
}