-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathkarabiner_ext.rb
143 lines (123 loc) · 3.06 KB
/
karabiner_ext.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
require_relative 'karabiner.rb'
module Karabiner
def self.rule(desc, manipulators:, **extra)
m = nil
if manipulators.is_a?(Array)
m = manipulators
else
m = [manipulators]
end
h = {
"description" => desc,
"manipulators" => m,
}
if extra.is_a?(Hash)
h.merge!(extra)
end
h
end
def self.manipulator(type = "basic")
h = {
"type" => type,
}
end
def self.device_identifier(vendor_id:, product_id:, desc: nil)
h = {
"vendor_id" => vendor_id,
"product_id" => product_id,
}
unless desc.to_s.empty?
h["description"] = desc
end
h
end
def self.device_if(*_identifiers)
identifiers = _identifiers.flatten
{
"type" => "device_if",
"identifiers" => identifiers,
}
end
def self.virtual_modifier_is(key)
variable_if(key, 1)
end
class << self
alias modifiers from_modifiers
end
def self.any_modifiers
modifiers(nil, ["any"])
end
end
module KarabinerEXT
def method_missing(m, *args, &block)
unless Karabiner.respond_to?(m)
super
end
Karabiner.send(m, *args, &block)
end
refine Array do
def if?(conditions)
self.each do |m|
m.if?(conditions)
end
self
end
end
refine Hash do
# method_missing won't work here https://bugs.ruby-lang.org/issues/13129
def from_key(code, modifiers = nil)
h = {
"key_code" => code,
}
h['modifiers'] = modifiers unless modifiers.nil?
from(h)
end
def from_modifiers(modifiers)
h = {
'modifiers' => modifiers
}
from(h)
end
def from(from)
set("from",from)
end
def to_key(code, **extra)
h = { "key_code" => code }
if extra.is_a?(Hash)
h.merge!(extra)
end
to(h)
end
def to(to)
set("to",to)
end
def set(key,value)
self[key] = value
self
end
def if?(conditions)
v = nil
if conditions.is_a?(Array)
v = conditions
else
v = [conditions]
end
set("conditions", v)
end
def to_if_alone(to_if_alone)
v = nil
if to_if_alone.is_a?(Hash)
v = to_if_alone
elsif to_if_alone.is_a?(String)
v = {key_code: to_if_alone}
else
raise "#{__method__} arg invalid"
end
set("to_if_alone", v)
end
def to_virtual_modifier(key)
to([Karabiner.set_variable(key, 1)])
set("to_after_key_up", [Karabiner.set_variable(key, 0)])
end
end
end