-
Notifications
You must be signed in to change notification settings - Fork 2
/
main.go
217 lines (172 loc) · 6.8 KB
/
main.go
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
package main
import (
"fmt"
"github.com/ahmetb/go-linq/v3"
"github.com/klyse/LogitechKeyboardLED/LogiKeyboard"
"github.com/klyse/LogitechKeyboardLED/LogiKeyboardTypes"
"github.com/klyse/LogitechKeyboardLED/Shortcuts"
"github.com/moutend/go-hook/pkg/keyboard"
"github.com/moutend/go-hook/pkg/types"
"log"
"os"
"os/signal"
"time"
)
var logiKeyboard *LogiKeyboard.LogiKeyboard
func main() {
log.SetFlags(0)
log.SetPrefix("error: ")
logiKeyboard = LogiKeyboard.Create()
defer logiKeyboard.Shutdown()
logiKeyboard.Init()
logiKeyboard.SetTargetDevice(LogiKeyboardTypes.LogiDeviceTypePerKeyRgb)
defaultLightning(false)
var shortcuts = []Shortcuts.Shortcut{
Shortcuts.CreateWithKey([]types.VKCode{types.VK_LSHIFT}, []Shortcuts.ShortcutKey{
Shortcuts.CreateKeyColorEffect(LogiKeyboardTypes.F6, 100, 0, 0, Shortcuts.Blinking),
Shortcuts.CreateKeyColor(LogiKeyboardTypes.F9, 0, 0, 100),
}),
Shortcuts.CreateWithKey([]types.VKCode{types.VK_LCONTROL}, []Shortcuts.ShortcutKey{
Shortcuts.CreateKeyColorEffect(LogiKeyboardTypes.F2, 100, 0, 0, Shortcuts.Blinking),
Shortcuts.CreateKeyColor(LogiKeyboardTypes.C, 100, 0, 100),
Shortcuts.CreateKeyColor(LogiKeyboardTypes.V, 100, 0, 100),
Shortcuts.CreateKeyColor(LogiKeyboardTypes.X, 100, 0, 0),
Shortcuts.CreateKeyColor(LogiKeyboardTypes.Y, 100, 0, 0),
Shortcuts.CreateKeyColor(LogiKeyboardTypes.Z, 100, 0, 0),
Shortcuts.CreateKeyColor(LogiKeyboardTypes.F, 0, 100, 0),
Shortcuts.CreateKeyColor(LogiKeyboardTypes.R, 100, 100, 0),
Shortcuts.CreateKeyColor(LogiKeyboardTypes.B, 0, 100, 0),
Shortcuts.CreateKeyColor(LogiKeyboardTypes.NUM_SLASH, 100, 0, 100),
Shortcuts.CreateKeyColor(LogiKeyboardTypes.HOME, 0, 100, 0),
Shortcuts.CreateKeyColor(LogiKeyboardTypes.END, 0, 100, 0),
Shortcuts.CreateKeyColor(LogiKeyboardTypes.W, 50, 0, 100),
Shortcuts.CreateKeyColor(LogiKeyboardTypes.F4, 100, 100, 0),
Shortcuts.CreateKeyColor(LogiKeyboardTypes.F8, 0, 0, 100),
}),
Shortcuts.CreateWithKey([]types.VKCode{types.VK_LCONTROL, types.VK_LSHIFT, types.VK_LMENU}, []Shortcuts.ShortcutKey{
Shortcuts.CreateKeyColor(LogiKeyboardTypes.T, 100, 0, 0),
}),
Shortcuts.CreateWithKey([]types.VKCode{types.VK_LCONTROL, types.VK_LMENU}, []Shortcuts.ShortcutKey{
Shortcuts.CreateKeyColor(LogiKeyboardTypes.B, 0, 100, 0),
Shortcuts.CreateKeyColor(LogiKeyboardTypes.L, 0, 100, 0),
}),
Shortcuts.CreateWithKey([]types.VKCode{types.VK_LCONTROL, types.VK_LSHIFT}, []Shortcuts.ShortcutKey{
Shortcuts.CreateKeyColor(LogiKeyboardTypes.Z, 0, 100, 0),
Shortcuts.CreateKeyColor(LogiKeyboardTypes.B, 0, 100, 0),
Shortcuts.CreateKeyColorEffect(LogiKeyboardTypes.F, 0, 100, 0, Shortcuts.Blinking),
Shortcuts.CreateKeyColor(LogiKeyboardTypes.R, 100, 100, 0),
}),
Shortcuts.CreateWithKey([]types.VKCode{types.VK_RMENU, types.VK_LCONTROL}, []Shortcuts.ShortcutKey{
Shortcuts.CreateKeyColor(LogiKeyboardTypes.SEVEN, 50, 50, 100),
Shortcuts.CreateKeyColor(LogiKeyboardTypes.ZERO, 50, 50, 100),
Shortcuts.CreateKeyColor(LogiKeyboardTypes.EIGHT, 50, 100, 0),
Shortcuts.CreateKeyColor(LogiKeyboardTypes.NINE, 50, 100, 0),
}),
Shortcuts.CreateWithKey([]types.VKCode{types.VK_LMENU}, []Shortcuts.ShortcutKey{
Shortcuts.CreateKeyColor(LogiKeyboardTypes.F4, 50, 50, 100),
Shortcuts.CreateKeyColorEffect(LogiKeyboardTypes.ONE, 0, 100, 0, Shortcuts.Blinking),
Shortcuts.CreateKeyColor(LogiKeyboardTypes.FIVE, 0, 0, 100),
}),
}
if err := run(shortcuts); err != nil {
log.Fatal(err)
}
}
func defaultLightning(preHotkey bool) {
fmt.Printf("defaultLighting %v\n", preHotkey)
logiKeyboard.StopEffects()
logiKeyboard.SetLightning(100, 100, 100)
logiKeyboard.SetLightingForKeyWithKeyName(LogiKeyboardTypes.LEFT_CONTROL, 100, 0, 0)
logiKeyboard.SetLightingForKeyWithKeyName(LogiKeyboardTypes.LEFT_SHIFT, 100, 0, 0)
logiKeyboard.SetLightingForKeyWithKeyName(LogiKeyboardTypes.LEFT_ALT, 100, 0, 0)
logiKeyboard.SetLightingForKeyWithKeyName(LogiKeyboardTypes.RIGHT_ALT, 100, 0, 0)
logiKeyboard.SetLightingForKeyWithKeyName(LogiKeyboardTypes.RIGHT_CONTROL, 100, 0, 0)
if !preHotkey {
logiKeyboard.SetLightingForKeyWithKeyName(LogiKeyboardTypes.F9, 0, 0, 100)
logiKeyboard.SetLightingForKeyWithKeyName(LogiKeyboardTypes.ESC, 0, 100, 0)
}
}
func run(shortcuts []Shortcuts.Shortcut) error {
// Buffer size is depends on your need. The 100 is placeholder value.
keyboardChan := make(chan types.KeyboardEvent, 100)
if err := keyboard.Install(nil, keyboardChan); err != nil {
return err
}
defer keyboard.Uninstall()
signalChan := make(chan os.Signal, 1)
signal.Notify(signalChan, os.Interrupt)
fmt.Println("start capturing keyboard input")
currentlyPressedKeys := make(map[types.VKCode]bool)
// get all relevant keys (modifiers)
var relevantKeys []types.VKCode
linq.From(shortcuts).
SelectManyT(func(c Shortcuts.Shortcut) linq.Query {
return linq.From(c.Modifiers())
}).
GroupByT(func(modifier types.VKCode) types.VKCode {
return modifier
}, func(modifier types.VKCode) types.VKCode {
return modifier
}).
SelectT(func(c linq.Group) types.VKCode {
return c.Key.(types.VKCode)
}).
ToSlice(&relevantKeys)
for {
select {
case <-time.After(5 * time.Minute):
fmt.Println("Received timeout signal")
return nil
case <-signalChan:
fmt.Println("Received shutdown signal")
return nil
case k := <-keyboardChan:
var prevState = currentlyPressedKeys[k.VKCode]
currentlyPressedKeys[k.VKCode] = k.Message == types.WM_KEYDOWN || k.Message == types.WM_SYSKEYDOWN
// if the key remained in the same state: ignore
if prevState == currentlyPressedKeys[k.VKCode] {
continue
}
// if the key is no modifier: ignore
if !linq.From(relevantKeys).
Contains(k.VKCode) {
continue
}
fmt.Printf("Received %v %v\n", k.Message, k.VKCode)
shortCut := linq.From(shortcuts).
WhereT(func(c Shortcuts.Shortcut) bool {
found := linq.From(c.Modifiers()).
AllT(func(y types.VKCode) bool {
return currentlyPressedKeys[y]
})
if !found {
return false
}
found = linq.From(currentlyPressedKeys).
WhereT(func(y linq.KeyValue) bool {
return y.Value.(bool)
}).
AnyWithT(func(y linq.KeyValue) bool {
return !linq.From(c.Modifiers()).
Contains(y.Key.(types.VKCode))
})
return !found
}).
First()
if shortCut != nil {
defaultLightning(true)
for _, logiKey := range shortCut.(Shortcuts.Shortcut).Keys() {
switch logiKey.Effect() {
case Shortcuts.Fixed:
logiKeyboard.SetLightingForKeyWithKeyName(logiKey.Key(), logiKey.Red(), logiKey.Green(), logiKey.Blue())
case Shortcuts.Blinking:
logiKeyboard.SetFlashSingleKey(logiKey.Key(), logiKey.Red(), logiKey.Green(), logiKey.Blue(), LogiKeyboardTypes.LogiLedDurationInfinite, 500)
}
}
} else {
defaultLightning(false)
}
}
continue
}
}