@@ -74,21 +74,20 @@ internal final class IOKeyEventMonitor {
74
74
let selfPtr = Unmanaged < IOKeyEventMonitor > . fromOpaque ( context!) . takeUnretainedValue ( )
75
75
let senderDevice = Unmanaged < IOHIDDevice > . fromOpaque ( sender!) . takeUnretainedValue ( )
76
76
77
- let vendorId = String ( describing: IOHIDDeviceGetProperty ( senderDevice, kIOHIDVendorIDKey as CFString ) )
78
- let productId = String ( describing: IOHIDDeviceGetProperty ( senderDevice, kIOHIDProductIDKey as CFString ) )
79
- let product = String ( describing: IOHIDDeviceGetProperty ( senderDevice, kIOHIDProductKey as CFString ) )
80
- let manufacturer = String ( describing: IOHIDDeviceGetProperty ( senderDevice, kIOHIDManufacturerKey as CFString ) )
81
- let serialNumber = String ( describing: IOHIDDeviceGetProperty ( senderDevice, kIOHIDSerialNumberKey as CFString ) )
82
- let locationId = String ( describing: IOHIDDeviceGetProperty ( senderDevice, kIOHIDLocationIDKey as CFString ) )
83
- let uniqueId = String ( describing: IOHIDDeviceGetProperty ( senderDevice, kIOHIDUniqueIDKey as CFString ) )
84
-
85
- let keyboard =
86
- selfPtr. useLocation ?
87
- " \( product) -[ \( vendorId) - \( productId) - \( manufacturer) - \( serialNumber) - \( locationId) ] " :
88
- " \( product) -[ \( vendorId) - \( productId) - \( manufacturer) - \( serialNumber) ] "
77
+ let vendorId = IOHIDDeviceGetProperty ( senderDevice, kIOHIDVendorIDKey as CFString ) ??? " unknown "
78
+ let productId = IOHIDDeviceGetProperty ( senderDevice, kIOHIDProductIDKey as CFString ) ??? " unknown "
79
+ let product = IOHIDDeviceGetProperty ( senderDevice, kIOHIDProductKey as CFString ) ??? " unknown "
80
+ let manufacturer = IOHIDDeviceGetProperty ( senderDevice, kIOHIDManufacturerKey as CFString ) ??? " unknown "
81
+ let serialNumber = IOHIDDeviceGetProperty ( senderDevice, kIOHIDSerialNumberKey as CFString ) ??? " unknown "
82
+ let locationId = IOHIDDeviceGetProperty ( senderDevice, kIOHIDLocationIDKey as CFString ) ??? " unknown "
83
+ let uniqueId = IOHIDDeviceGetProperty ( senderDevice, kIOHIDUniqueIDKey as CFString ) ??? " unknown "
84
+
85
+ let keyboard = selfPtr. useLocation
86
+ ? " \( product) -[ \( vendorId) - \( productId) - \( manufacturer) - \( serialNumber) - \( locationId) ] "
87
+ : " \( product) -[ \( vendorId) - \( productId) - \( manufacturer) - \( serialNumber) ] "
89
88
90
89
if selfPtr. verbosity >= TRACE {
91
- print ( " received event from keyboard \( keyboard) - \( locationId) - \( uniqueId) " )
90
+ print ( " received event from keyboard \( keyboard) - \( locationId) - \( uniqueId) " )
92
91
}
93
92
selfPtr. onKeyboardEvent ( keyboard: keyboard)
94
93
}
@@ -171,3 +170,11 @@ extension IOKeyEventMonitor {
171
170
}
172
171
}
173
172
}
173
+
174
+ // Nicer string interpolation of optional strings, see: https://oleb.net/blog/2016/12/optionals-string-interpolation/
175
+
176
+ infix operator ??? : NilCoalescingPrecedence
177
+
178
+ public func ??? < T> ( optional: T ? , defaultValue: @autoclosure ( ) -> String ) -> String {
179
+ return optional. map { String ( describing: $0) } ?? defaultValue ( )
180
+ }
0 commit comments