-
Notifications
You must be signed in to change notification settings - Fork 5
/
remap_hyper.swift
47 lines (40 loc) · 977 Bytes
/
remap_hyper.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
import Foundation
// Annoyingly, IOKIt's HID headers are not exposed directly
func hidutil(properties: some Codable) throws {
let value = String(data: try JSONEncoder().encode(properties), encoding: .utf8)!
let process = Process()
process.executableURL = URL(fileURLWithPath: "/usr/bin/hidutil")
process.arguments = ["property", "--set", value]
process.launch()
process.waitUntilExit()
}
func setup() throws {
try hidutil(properties: [
"UserKeyMapping": [
[
"HIDKeyboardModifierMappingSrc": 0x7_0000_0039, // Caps lock
"HIDKeyboardModifierMappingDst": 0x7_0000_006D, // F18
]
]
])
}
func teardown() throws {
try hidutil(properties: [
"UserKeyMapping": [
[String: Int](),
]
])
}
try setup()
let signals = [SIGINT, SIGTERM]
let sources = signals.map {
signal($0, SIG_IGN)
let source = DispatchSource.makeSignalSource(signal: $0)
source.setEventHandler {
try? teardown()
exit(0)
}
source.resume()
return source
}
dispatchMain()