Skip to content

Commit 1390e26

Browse files
Caldisclaude
andcommitted
feat: unify Logitech CID name mapping with Solaar registry (~310 entries)
Consolidate 3 duplicate CID→name hardcoded mappings into a single LogitechCIDRegistry (sourced from Solaar project's special_keys.py). Eliminates "Logi(2253)"-style fallback names for unknown buttons. - Add LogitechCIDRegistry.swift with ~310 CID entries from Solaar - Remove LogitechCIDMap from MosInputEvent.swift - Remove HIDPPInfo.cidNames from LogitechHIDDebugPanel.swift - Remove Logi entries from KeyCode.mouseMap - Update all consumers (9 files) to use LogitechCIDRegistry - Add Logi code fallback to ScrollHotkey.displayName Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
1 parent 757f350 commit 1390e26

File tree

11 files changed

+1448
-78
lines changed

11 files changed

+1448
-78
lines changed

Mos/Components/BrandTag.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ struct BrandTag {
3131

3232
/// 按键码是否属于某个品牌
3333
static func isLogiCode(_ code: UInt16) -> Bool {
34-
return LogitechCIDMap.isLogitechCode(code)
34+
return LogitechCIDRegistry.isLogitechCode(code)
3535
}
3636

3737
/// 快捷键 ID 是否属于某个品牌

Mos/InputEvent/MosInputEvent.swift

Lines changed: 3 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -48,59 +48,6 @@ struct DeviceFilter: Codable, Equatable {
4848
}
4949
}
5050

51-
// MARK: - LogitechCIDMap
52-
/// Logitech CID -> Mos 按钮码映射
53-
/// 标准 CGEvent 鼠标按钮: 0~31, Logitech HID++ 专有: 1000+
54-
struct LogitechCIDMap {
55-
private static let cidToCode: [UInt16: UInt16] = [
56-
0x0050: 1003, // Left Click (diverted)
57-
0x0051: 1004, // Right Click (diverted)
58-
0x0052: 1005, // Middle Click (diverted)
59-
0x0053: 1006, // Back (diverted)
60-
0x0056: 1007, // Forward (diverted)
61-
0x00C3: 1000, // Gesture Button
62-
0x00C4: 1001, // SmartShift
63-
0x00D7: 1002, // DPI Change Button
64-
]
65-
66-
static func toMosCode(_ cid: UInt16) -> UInt16 {
67-
if let known = cidToCode[cid] { return known }
68-
let mapped = UInt32(2000) + UInt32(cid)
69-
return mapped <= UInt32(UInt16.max) ? UInt16(mapped) : UInt16(cid & 0x0FFF) + 2000
70-
}
71-
72-
static func displayName(forCode code: UInt16) -> String {
73-
switch code {
74-
case 1000: return "Gesture"
75-
case 1001: return "SmartShift"
76-
case 1002: return "DPI"
77-
case 1003: return "Left Click"
78-
case 1004: return "Right Click"
79-
case 1005: return "Middle Click"
80-
case 1006: return "Back"
81-
case 1007: return "Forward"
82-
default: return "Logi(\(code))"
83-
}
84-
}
85-
86-
/// 判断按钮码是否属于 Logitech HID++ 专有范围
87-
static func isLogitechCode(_ code: UInt16) -> Bool {
88-
return code >= 1000
89-
}
90-
91-
/// 反向映射: Mos code → CID
92-
static func toCID(_ mosCode: UInt16) -> UInt16? {
93-
if let entry = cidToCode.first(where: { $0.value == mosCode }) {
94-
return entry.key
95-
}
96-
// 反向 fallback (2000 + cid)
97-
if mosCode >= 2000 {
98-
return mosCode - 2000
99-
}
100-
return nil
101-
}
102-
}
103-
10451
// MARK: - MosInputEvent
10552
/// 统一输入事件 (运行时对象, 不可序列化)
10653
struct MosInputEvent {
@@ -155,8 +102,8 @@ struct MosInputEvent {
155102
case .keyboard:
156103
components.append(KeyCode.keyMap[code] ?? "Key(\(code))")
157104
case .mouse:
158-
if LogitechCIDMap.isLogitechCode(code) {
159-
components.append(LogitechCIDMap.displayName(forCode: code))
105+
if LogitechCIDRegistry.isLogitechCode(code) {
106+
components.append(LogitechCIDRegistry.name(forMosCode: code))
160107
components.append("[Logi]") // 特殊标记, KeyPreview 渲染为 tag
161108
} else {
162109
components.append(KeyCode.mouseMap[code] ?? "Mouse(\(code))")
@@ -184,7 +131,7 @@ struct MosInputEvent {
184131
if !hasModifiers { return false }
185132
return true
186133
case .mouse:
187-
if LogitechCIDMap.isLogitechCode(code) { return true }
134+
if LogitechCIDRegistry.isLogitechCode(code) { return true }
188135
if KeyCode.mouseMainKeys.contains(code) { return hasModifiers }
189136
return true
190137
}

Mos/Keys/KeyCode.swift

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -162,10 +162,6 @@ struct KeyCode {
162162
9: "🖱9", 10: "🖱10", 11: "🖱11", 12: "🖱12", 13: "🖱13",
163163
14: "🖱14", 15: "🖱15", 16: "🖱16", 17: "🖱17", 18: "🖱18",
164164
19: "🖱19", 20: "🖱20",
165-
// Logitech HID++ 专有按键
166-
1000: "Gesture", 1001: "SmartShift", 1002: "DPI",
167-
1003: "Left Click", 1004: "Right Click",
168-
1005: "Middle Click", 1006: "Back", 1007: "Forward",
169165
]
170166
static let mouseMainKeys: [UInt16] = [0,1] // Only protect left/right clicks, allow middle button without modifiers
171167
}

0 commit comments

Comments
 (0)