State.lastFired의 타입을 UInt8에서 CGKeyCode로 변경하고, tiltMask 계산 로직을 개선하여 마우스의 틸트 모드에 따른 키 코드 매핑을 명확히 함
This commit is contained in:
+9
-5
@@ -17,7 +17,7 @@ struct ReportSource: Hashable {
|
|||||||
final class State {
|
final class State {
|
||||||
var buffers: [IOHIDDevice: UnsafeMutablePointer<UInt8>] = [:]
|
var buffers: [IOHIDDevice: UnsafeMutablePointer<UInt8>] = [:]
|
||||||
var lastBits: [ReportSource: UInt8] = [:]
|
var lastBits: [ReportSource: UInt8] = [:]
|
||||||
var lastFired: [UInt8: TimeInterval] = [:]
|
var lastFired: [CGKeyCode: TimeInterval] = [:]
|
||||||
var opened = false
|
var opened = false
|
||||||
var lastStatus = ""
|
var lastStatus = ""
|
||||||
}
|
}
|
||||||
@@ -37,7 +37,11 @@ func sendCommandKey(_ keyCode: CGKeyCode) {
|
|||||||
up.post(tap: .cgSessionEventTap)
|
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).
|
// Ignore a second press of the same tilt within this window (switch bounce).
|
||||||
let debounceInterval: TimeInterval = 0.12
|
let debounceInterval: TimeInterval = 0.12
|
||||||
|
|
||||||
@@ -52,9 +56,9 @@ let reportCallback: IOHIDReportCallback = {
|
|||||||
let pressed = value & ~(state.lastBits[source] ?? 0)
|
let pressed = value & ~(state.lastBits[source] ?? 0)
|
||||||
state.lastBits[source] = value
|
state.lastBits[source] = value
|
||||||
let now = ProcessInfo.processInfo.systemUptime
|
let now = ProcessInfo.processInfo.systemUptime
|
||||||
for (mask, keyCode) in [(UInt8(0x20), CGKeyCode(33)), (UInt8(0x40), CGKeyCode(30))] where pressed & mask != 0 {
|
for (mask, keyCode) in [(leftTiltMask, CGKeyCode(33)), (rightTiltMask, CGKeyCode(30))] where pressed & mask != 0 {
|
||||||
if let last = state.lastFired[mask], now - last < debounceInterval { continue }
|
if let last = state.lastFired[keyCode], now - last < debounceInterval { continue }
|
||||||
state.lastFired[mask] = now
|
state.lastFired[keyCode] = now
|
||||||
sendCommandKey(keyCode)
|
sendCommandKey(keyCode)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user