This repository was archived by the owner on Mar 1, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathautoremap.py
More file actions
105 lines (77 loc) · 2.73 KB
/
Copy pathautoremap.py
File metadata and controls
105 lines (77 loc) · 2.73 KB
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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
from typing import Dict, List, Tuple
from json import dumps, loads
import argparse
import subprocess
import time
import usb
KEYBOARD_NAME = 'MX Keys'
VID = 0x046d
PID = 0xc52b
GENERAL_MAPPINGS = [
(0x7000000e6, 0x7000000e7), # Swap right CMD with OPT
]
MX_KEYS_MAPPINGS = [
(0x700000064, 0x700000035), # Swap §/£ with `/~
]
DEFAULT_INTERVAL = 10
Mapping = Dict[str, int]
def _make_mapping(src: int, dst: int) -> Mapping:
d: Mapping = dict()
d['HIDKeyboardModifierMappingSrc'] = src
d['HIDKeyboardModifierMappingDst'] = dst
return d
def make_mapping(pair: Tuple[int, int]) -> List[Mapping]:
a, b = pair
return [_make_mapping(a, b), _make_mapping(b, a)]
def set_mappings(mappings: List[Mapping]):
mappinging_dict: Dict[str, List[Mapping]] = dict()
mappinging_dict['UserKeyMapping'] = mappings
subprocess.Popen(args=['hidutil', 'property', '--set',
dumps(mappinging_dict)],
stdout=subprocess.PIPE).communicate()
def is_keyboard_connected_bt(name: str) -> bool:
stdout, _ = subprocess.Popen(args=['system_profiler',
'SPBluetoothDataType',
'-json'],
stdout=subprocess.PIPE).communicate()
out = loads(stdout.decode())
try:
connected_devices = out['SPBluetoothDataType'][0]['device_connected']
for device in connected_devices:
if name in device:
return True
except KeyError:
return False
return False
def is_keyboard_connected_usb(vid: int, pid: int) -> bool:
device: usb.Device | None = usb.core.find(idVendor=vid, idProduct=pid)
if device is not None:
return True
return False
def is_keyboard_connected() -> bool:
bt_conn = is_keyboard_connected_bt(KEYBOARD_NAME)
usb_conn = is_keyboard_connected_usb(VID, PID)
return bt_conn or usb_conn
def update_mapping(keyboard_connected: bool):
print(f'update: {keyboard_connected=}')
mappings: List[Mapping] = list()
for x in GENERAL_MAPPINGS:
mappings.extend(make_mapping(x))
if keyboard_connected:
for x in MX_KEYS_MAPPINGS:
mappings.extend(make_mapping(x))
set_mappings(mappings)
def main():
parser = argparse.ArgumentParser()
parser.add_argument('-i', '--interval', type=int, default=DEFAULT_INTERVAL)
args = parser.parse_args()
prev_connected = is_keyboard_connected()
update_mapping(prev_connected)
while True:
time.sleep(args.interval)
connected = is_keyboard_connected()
if connected != prev_connected:
update_mapping(connected)
prev_connected = connected
if __name__ == '__main__':
main()