-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathkarabiner.rb
514 lines (468 loc) · 10.6 KB
/
karabiner.rb
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
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
if File.exist?("./private.rb")
require "./private"
end
module Rule
module_function
def gen(desc, manipulators)
{
description: desc,
manipulators: manipulators.map do |m| m.merge({
type: "basic"
})
end
}
end
end
module Cond
module_function
def gen_device_if(identifiers)
{
type: "device_if",
identifiers: identifiers,
}
end
def gen_variable_if(name, value, is = true)
{
name: name,
type: "variable_#{is ? 'if' : 'unless'}",
value: value
}
end
def gen_input_if(src, is = true)
{
type: "input_source_#{is ? 'if' : 'unless'}",
input_sources: [
language: src
]
}
end
# module variables
def is_internal_keyboard
gen_device_if([
{vendor_id: 1452, product_id: 610},
])
end
# layers
def is_layer0
gen_variable_if("layer", 0)
end
def is_layer1
gen_variable_if("layer", 1)
end
def is_layer2
gen_variable_if("layer", 2)
end
def is_layer3
gen_variable_if("layer", 3)
end
def input_is_en
gen_input_if("en")
end
def input_is_zh
gen_input_if("zh-Hans")
end
def input_is_ja
gen_input_if("ja")
end
end
module SetVar
module_function
def gen(name, value)
{
set_variable: {
name: name,
value: value,
}
}
end
end
module ModFrom
module_function
def gen_mandatory(mods)
{
mandatory: mods
}
end
def gen_optional(mods)
{
optional: mods
}
end
# module variables
def optional_any
gen_optional(["any"])
end
def optional_not_fn
gen_optional([
"control",
"option",
"shift",
"command",
])
end
def optional_not_shift
gen_optional([
"control",
"option",
"command",
])
end
def mandatory_fn
gen_mandatory(["fn"])
end
def mandatory_control
gen_mandatory(["control"])
end
def mandatory_command
gen_mandatory(["command"])
end
def mandatory_control_command
gen_mandatory(["control", "command"])
end
def mandatory_option
gen_mandatory(["option"])
end
def mandatory_shift
gen_mandatory(["shift"])
end
end
module KeyRegion
module_function
def alphabet
('a'..'z').to_a
end
# module variables
def symbol_below
[
"comma",
"period",
"slash",
"semicolon",
"quote",
"open_bracket",
"close_bracket",
"backslash",
]
end
def symbol_above
[
"hyphen",
"equal_sign",
]
end
def others
[
"return_or_enter"
]
end
def number
('1'..'9').to_a << '0'
end
def below
alphabet + symbol_below
end
def above
number + symbol_above
end
def whole
below + above + others
end
end
class Layer
@@cond_is_in_layer0 = {
name: "layer",
type: "variable_if",
value: 0,
}
def initialize(
layer_num, layer_trigger,
layer_mods, layer_keys,
layer_trigger_mod = ModFrom.optional_any
)
@layer_num = layer_num
@layer_trigger = layer_trigger
@layer_mods = layer_mods
@layer_keys = layer_keys
@layer_trigger_mod = layer_trigger_mod
@layer_keymap = layer_keys.zip(layer_keys).to_h
@layer_keymap_filter = []
@layer_description_prefix = "layer #{@layer_num}"
@rules = []
@cond_is_in_this_layer = {
name: "layer",
type: "variable_if",
value: @layer_num,
}
end
def gen_rules
gen_rules_hook_before()
@rules << trigger_rule
@rules << main_rule
gen_rules_hook_after()
@rules
end
def trigger_rule
Rule.gen(
"#{@layer_description_prefix}: toggle layer",
[{
conditions: [
Cond.is_layer0,
],
from: {
key_code: @layer_trigger,
modifiers: @layer_trigger_mod,
},
to: [SetVar.gen("layer", @layer_num)],
to_after_key_up: [SetVar.gen("layer", 0)],
to_if_alone: [{
key_code: @layer_trigger,
}]
}]
)
end
def main_rule()
@layer_keymap_filter.each { |k| @layer_keymap.delete(k) }
Rule.gen(
"#{@layer_description_prefix}: key -> layer_mod-key",
@layer_keymap.map { |from_key, to_key|
layer_manipulator(from_key, to_key)
}
)
end
def layer_manipulator(from_key, to_key, mod_from = ModFrom.optional_any)
{
conditions: [
@cond_is_in_this_layer
],
from: {
key_code: from_key,
modifiers: mod_from
},
to: [
key_code: to_key,
modifiers: @layer_mods
]
}
end
def gen_rules_hook_before
end
def gen_rules_hook_after
end
end
# Basic movements
class Layer1 < Layer
def initialize(*args)
super(1, *args)
end
def gen_rules_hook_before
# switch input method
def input_source_rule_hook
# key => input_source_to_switch_to
key_maps = [
['a', :en],
['s', :zh],
['d',:ja],
]
@layer_keymap_filter += key_maps.map { |k, _| k }
# place entry following the order in system keyboard
cond_map = {
en: Cond.input_is_en,
ja: Cond.input_is_ja,
zh: Cond.input_is_zh,
}
sys_next_key = "f18"
len = key_maps.length
find_src_index = lambda do |src|
cond_map.find_index { |k, _| k == src }
end
manipulators = (0...len).to_a
.product((0...len).to_a)
.map do |(index_to, index_from)|
_, src_from = key_maps[index_from]
trigger_key, src_to = key_maps[index_to]
pos_from = find_src_index.call(src_from)
pos_to = find_src_index.call(src_to)
steps = (pos_to - pos_from + len) % len
{
conditions: [
@cond_is_in_this_layer,
cond_map[src_from],
],
from: {
key_code: trigger_key,
modifiers: ModFrom.optional_any,
},
to: Array.new(steps, { key_code: sys_next_key})
}
end
@rules << Rule.gen(
"#{@layer_description_prefix}: switch input source",
manipulators
)
end
def wm_rule_hook()
key_map = KeyRegion.number.zip(('1'..'10').map { |x| 'f' + x }).to_h
manipulators = key_map.map do |from_key, to_key|
m = layer_manipulator(from_key, to_key, mod_from = ModFrom.mandatory_shift)
m
end
@rules << Rule.gen(
"#{@layer_description_prefix}: windows manager override",
manipulators
)
end
# Cursor movement
def movement_rule_hook()
# from_key_code, [(to_key_code, to_motifiers)...]
move_key_maps = [
['h', [['left_arrow', []]]],
['j', [['down_arrow', []]]],
['k', [['up_arrow', []]]],
['l', [['right_arrow', []]]],
['u', [['page_down', []]]],
['i', [['page_up', []]]],
['p', [['f6', []]]],
['n', [['delete_or_backspace', []]]],
['m', [['delete_forward', []]]],
['y', [['a', ['control']]]],
['o', [['e', ['control']]]],
['semicolon', [['left_arrow',['option']]]],
['quote', [['right_arrow', ['option']]]],
['comma', [['left_arrow', ['option', 'shift']], ['delete_or_backspace', []]]],
['period', [['right_arrow', ['option', 'shift']], ['delete_or_backspace', []]]],
]
# (from_key_code, to_key_code, apps)
move_app_specific_key_maps = [
['y', 'home', ['^com\\.tdesktop\\.Telegram$']],
['o', 'end', ['^com\\.tdesktop\\.Telegram$']],
]
@layer_keymap_filter += (move_key_maps + move_app_specific_key_maps).map { |k, _| k }
move_manipulators = move_key_maps.map do |f, t| {
conditions: [
@cond_is_in_this_layer,
],
from: {
key_code: f,
modifiers: ModFrom.optional_any,
},
to: t.map do | key, mods | {
key_code: key,
modifiers: (mods if !mods.empty?)
}.reject{ |k, v| v.nil? }
end
}
end
move_app_specific_manipulators = move_app_specific_key_maps.map do |f, t, c| {
conditions: [
@cond_is_in_this_layer,
{
"bundle_identifiers": c,
"type": "frontmost_application_if"
}
],
from: {
key_code: f,
modifiers: ModFrom.optional_any
},
to: [
{key_code: t}
]
}
end
@rules << Rule.gen(
"#{@layer_description_prefix}: app specific movements",
move_app_specific_manipulators
)
@rules << Rule.gen(
"#{@layer_description_prefix}: general movements",
move_manipulators
)
end
input_source_rule_hook()
wm_rule_hook()
movement_rule_hook()
end
end
# Windows Management
class Layer2 < Layer
def initialize(*args)
super(2, *args)
end
def gen_rules_hook_before
def private_rule_hook
if defined?(Private)
@rules << Rule.gen(
"#{@layer_description_prefix}: private password",
[{
conditions: [
@cond_is_in_this_layer,
],
from: {
key_code: "grave_accent_and_tilde",
},
to:
(Private.password.chars.map do |c|
{key_code: c}
end) << {key_code: "return_or_enter"}
}]
)
end
end
def f_region_rule_hook
# F Region
f_map = (KeyRegion.above
.zip(('1'..'12').map { |x| 'f' + x })).to_h
@layer_keymap_filter += f_map.keys
manipulators = f_map.map do |from_key, to_key|
m = layer_manipulator(from_key, to_key)
m[:to][0].delete(:modifiers)
m
end
@rules << Rule.gen(
"#{@layer_description_prefix}: F region",
manipulators
)
end
private_rule_hook()
f_region_rule_hook()
end
end
# F Region, System Control, Media
class Layer3 < Layer
def initialize(*args)
super(3, *args)
end
def gen_rules_hook_before
def app_rule_hook
# Clementine
app_map = {
"j" => "5",
"k" => "6",
"l" => "7",
"n" => "8",
"m" => "9",
}
@layer_keymap.merge!(app_map)
end
def media_rule_hook
# Media
media_map = {
"o" => "volume_decrement",
"p" => "volume_increment",
"i" => "mute",
}
@layer_keymap_filter += media_map.keys
manipulators = media_map.map do |from_key, to_key|
m = layer_manipulator(from_key, to_key)
m[:to][0].delete(:modifiers)
m
end
@rules << Rule.gen(
"#{@layer_description_prefix}: media, F region",
manipulators
)
end
app_rule_hook()
media_rule_hook()
end
end