-
Notifications
You must be signed in to change notification settings - Fork 1
/
xkeysnail.py
213 lines (200 loc) · 7 KB
/
xkeysnail.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
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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
# See https://github.com/mooz/xkeysnail
# Usage: sudo /usr/local/bin/xkeysnail ~/config/xkeysnail.py
# - possibly nohup ... &>/dev/null &
import re
from xkeysnail.transform import *
# [Global modmap] Change modifier keys as in xmodmap
define_modmap({
Key.CAPSLOCK: Key.LEFT_CTRL,
})
# # [Conditional modmap] Change modifier keys in certain applications
# define_conditional_modmap(re.compile(r'Emacs'), {
# Key.RIGHT_CTRL: Key.ESC,
# })
# # [Multipurpose modmap] Give a key two meanings. A normal key when pressed and
# # released, and a modifier key when held down with another key. See Xcape,
# # Carabiner and caps2esc for ideas and concept.
# define_multipurpose_modmap(
# # Enter is enter when pressed and released. Control when held down.
# {Key.ENTER: [Key.ENTER, Key.RIGHT_CTRL]}
#
# # Capslock is escape when pressed and released. Control when held down.
# # {Key.CAPSLOCK: [Key.ESC, Key.LEFT_CTRL]
# # To use this example, you can't remap capslock with define_modmap.
# )
# # Keybindings for Firefox/Chrome
# define_keymap(re.compile("Firefox|Google-chrome"), {
# # Ctrl+Alt+j/k to switch next/previous tab
# K("C-M-j"): K("C-TAB"),
# K("C-M-k"): K("C-Shift-TAB"),
# # Type C-j to focus to the content
# K("C-j"): K("C-f6"),
# # very naive "Edit in editor" feature (just an example)
# K("C-o"): [K("C-a"), K("C-c"), launch(["gedit"]), sleep(0.5), K("C-v")]
# }, "Firefox and Chrome")
# # Keybindings for Zeal https://github.com/zealdocs/zeal/
# define_keymap(re.compile("Zeal"), {
# # Ctrl+s to focus search area
# K("C-s"): K("C-k"),
# }, "Zeal")
# Emacs-like keybindings in non-Emacs applications
#define_keymap(lambda wm_class: wm_class not in ("Emacs", "URxvt", "Alacritty"), {
define_keymap(re.compile(r"Google-chrome"), {
# # Cursor
# K("C-b"): with_mark(K("left")),
# K("C-f"): with_mark(K("right")),
# K("C-p"): with_mark(K("up")),
# K("C-n"): with_mark(K("down")),
# K("C-h"): with_mark(K("backspace")),
# # Forward/Backward word
# K("M-b"): with_mark(K("C-left")),
# K("M-f"): with_mark(K("C-right")),
# Beginning/End of line
K("C-a"): with_mark(K("home")),
K("C-Shift-a"): K("C-Shift-a"), # don't override this one (vscode)
K("C-e"): with_mark(K("end")),
K("C-Shift-e"): K("C-Shift-e"), # don't override this one (vscode)
## Page up/down
#K("M-v"): with_mark(K("page_up")),
#K("C-v"): with_mark(K("page_down")),
# Beginning/End of file
K("M-Shift-comma"): with_mark(K("C-home")),
K("M-Shift-dot"): with_mark(K("C-end")),
# # Newline
# K("C-m"): K("enter"),
# K("C-j"): K("enter"),
# K("C-o"): [K("enter"), K("left")],
# Copy
# K("C-w"): [K("C-x"), set_mark(False)],
# K("M-w"): [K("C-c"), set_mark(False)],
# K("C-y"): [K("C-v"), set_mark(False)],
# Delete
K("C-d"): [K("delete"), set_mark(False)],
K("M-d"): [K("C-delete"), set_mark(False)],
K("C-Shift-d"): K("C-Shift-d"), # preserve just in case
# Kill line
K("C-k"): [K("Shift-end"), K("C-x"), set_mark(False)],
K("C-Shift-k"): K("C-Shift-k"), # preserve just in case
# # Undo
# K("C-slash"): [K("C-z"), set_mark(False)],
# K("C-Shift-ro"): K("C-z"),
# # Mark
# K("C-space"): set_mark(True),
# K("C-M-space"): with_or_set_mark(K("C-right")),
# Search
#K("C-s"): [K("C-f"), K("enter")], # K("F3"),
#K("C-r"): K("Shift-C-g"), # K("Shift-F3"),
#K("C-s"): K("F3"),
#K("C-r"): K("Shift-F3"),
K("M-Shift-key_5"): K("C-h"),
# Cancel
K("C-g"): [K("esc"), set_mark(False)],
K("C-Shift-g"): K("C-Shift-g"), # preserve just in case
# Escape
K("C-q"): escape_next_key,
K("C-Shift-q"): K("C-Shift-q"), # preserve just in case
# C-x YYY
#K("C-x"): {
# # # C-x h (select all)
# # K("h"): [K("C-home"), K("C-a"), set_mark(True)],
# # # C-x C-f (open)
# # K("C-f"): K("C-o"),
# # C-x C-s (save)
# K("C-s"): K("C-s"),
# # C-x k (kill tab)
# K("k"): K("C-f4"),
# # # C-x C-c (exit)
# # K("C-c"): K("C-q"),
# # cancel
# K("C-g"): pass_through_key,
# # C-x u (undo)
# K("u"): [K("C-z"), set_mark(False)],
# # C-x w (delete)
# K("w"): K("delete"),
# # C-x C-x: cut
# K("C-x"): K("C-x"),
# # C-x C-v: paste
# K("C-v"): K("C-v"),
#},
# Suppress bad things I never want to do
K("Shift-C-w"): None, # close window !!
K("M-q"): None, # close all windows !!
K("C-w"): K("C-f4"), # more effective
K("M-Shift-x"): K("M-Shift-key_5"), # strike-through in docs
}, "Emacs-like keys")
# NOTE: this doesn't really work, since it depends too heavily on mouse position
#define_keymap(re.compile(r".*"), {
# K("Shift-Insert"): K("BTN_MIDDLE"),
#}, "keyboard paste")
define_keymap(re.compile(r"Code"), {
# # Cursor
# K("C-b"): with_mark(K("left")),
# K("C-f"): with_mark(K("right")),
# K("C-p"): with_mark(K("up")),
# K("C-n"): with_mark(K("down")),
# K("C-h"): with_mark(K("backspace")),
# # Forward/Backward word
# K("M-b"): with_mark(K("C-left")),
# K("M-f"): with_mark(K("C-right")),
# Beginning/End of line
K("C-a"): with_mark(K("home")),
K("C-e"): with_mark(K("end")),
## Page up/down
#K("M-v"): with_mark(K("page_up")),
#K("C-v"): with_mark(K("page_down")),
# Beginning/End of file
K("M-Shift-comma"): with_mark(K("C-home")),
K("M-Shift-dot"): with_mark(K("C-end")),
# # Newline
# K("C-m"): K("enter"),
# K("C-j"): K("enter"),
# K("C-o"): [K("enter"), K("left")],
# Copy
# K("C-w"): [K("C-x"), set_mark(False)],
# K("M-w"): [K("C-c"), set_mark(False)],
# K("C-y"): [K("C-v"), set_mark(False)],
# Delete
K("C-d"): [K("delete"), set_mark(False)],
K("M-d"): [K("C-delete"), set_mark(False)],
# Kill line
K("C-k"): [K("Shift-end"), K("C-x"), set_mark(False)],
# # Undo
# K("C-slash"): [K("C-z"), set_mark(False)],
# K("C-Shift-ro"): K("C-z"),
# # Mark
# K("C-space"): set_mark(True),
# K("C-M-space"): with_or_set_mark(K("C-right")),
# Search
#K("C-s"): [K("C-f"), K("enter")], # K("F3"),
#K("C-r"): K("Shift-C-g"), # K("Shift-F3"),
#K("C-s"): K("F3"),
#K("C-r"): K("Shift-F3"),
K("M-Shift-key_5"): K("C-h"),
# Cancel
K("C-g"): [K("esc"), set_mark(False)],
# Escape
K("C-q"): escape_next_key,
# C-x YYY
#K("C-x"): {
# # # C-x h (select all)
# # K("h"): [K("C-home"), K("C-a"), set_mark(True)],
# # # C-x C-f (open)
# # K("C-f"): K("C-o"),
# # C-x C-s (save)
# K("C-s"): K("C-s"),
# # C-x k (kill tab)
# K("k"): K("C-f4"),
# # # C-x C-c (exit)
# # K("C-c"): K("C-q"),
# # cancel
# K("C-g"): pass_through_key,
# # C-x u (undo)
# K("u"): [K("C-z"), set_mark(False)],
# # C-x w (delete)
# K("w"): K("delete"),
# # C-x C-x: cut
# K("C-x"): K("C-x"),
# # C-x C-v: paste
# K("C-v"): K("C-v"),
#},
}, "Emacs-like keys")