-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathkeyutil.py
58 lines (49 loc) · 1.17 KB
/
keyutil.py
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
from Xlib.keysymdef import latin1
from functools import reduce
from Xlib import X
def get_posmap(keymap, disp):
posmap = {}
for i, row in enumerate(keymap):
for j, key in enumerate(row):
posmap[keycode(key, disp)] = (i, j)
return posmap
def initkeys(keymap, disp, root, mask=None):
return [
initkey(
keycode(key, disp),
root,
mask
) for key in reduce(lambda x, y: x + y, keymap)
]
def initkey(code, root, mask=None):
if mask == 'Windows':
root.grab_key(
code,
X.Mod4Mask,
1,
X.GrabModeAsync,
X.GrabModeAsync
)
else:
root.grab_key(
code,
X.ControlMask | X.Mod1Mask,
1,
X.GrabModeAsync,
X.GrabModeAsync
)
root.grab_key(
code,
X.ControlMask | X.Mod1Mask | X.Mod2Mask,
1,
X.GrabModeAsync,
X.GrabModeAsync
)
return code
def keycode(key, disp):
return disp.keysym_to_keycode(
getattr(
latin1,
'XK_{}'.format(key)
)
)