@@ -33,13 +33,15 @@ type Message struct {
33
33
34
34
func main () {
35
35
var (
36
- listDevices = flag .Bool ("list" , false , "list all devices listened by evdev" )
37
- printMode = flag .Bool ("print" , false , "print pressed keys" )
38
- quietMode = flag .Bool ("quiet" , false , "be silent" )
39
- deviceMatch = flag .String ("match" , "keyboard" , "regexp used to match keyboard device" )
40
- keysymFirst = flag .String ("first" , "LEFTSHIFT" , "key used for switcing on first xkb group" )
41
- keysymSecond = flag .String ("second" , "RIGHTSHIFT" , "key used for switcing on second xkb group" )
42
- scanOnce = flag .Bool ("scan-once" , false , "scan for devices only at startup (less power consumption)" )
36
+ listDevices = flag .Bool ("list" , false , "list all devices listened by evdev" )
37
+ printMode = flag .Bool ("print" , false , "print pressed keys" )
38
+ quietMode = flag .Bool ("quiet" , false , "be silent" )
39
+ deviceMatch = flag .String ("match" , "keyboard" , "regexp used to match keyboard device" )
40
+ keysymFirst = flag .String ("first" , "LEFTSHIFT" , "key used for switching on first xkb group" )
41
+ keysymSecond = flag .String ("second" , "RIGHTSHIFT" , "key used for switching on second xkb group" )
42
+ scanOnce = flag .Bool ("scan-once" , false , "scan for devices only at startup (less power consumption)" )
43
+ dblKeystroke = flag .Bool ("double-keystroke" , false , "require pressing the same key twice to switch the layout" )
44
+ dblKeyTimeout = flag .Int ("double-keystroke-timeout" , 500 , "second keystroke timeout in milliseconds" )
43
45
)
44
46
45
47
flag .Parse ()
@@ -86,6 +88,7 @@ func main() {
86
88
display , keyFirst , keySecond ,
87
89
* printMode , * quietMode ,
88
90
reDeviceMatch , * scanOnce ,
91
+ * dblKeystroke , * dblKeyTimeout ,
89
92
)
90
93
91
94
<- terminate
@@ -213,13 +216,17 @@ func listenKeyboards(
213
216
display * C.Display ,
214
217
keyFirst uint16 , keySecond uint16 ,
215
218
printMode , quietMode bool , deviceMatch * regexp.Regexp ,
216
- scanOnce bool ,
219
+ scanOnce bool , dblKeystroke bool , dblKeyTimeout int ,
217
220
) {
218
221
var groupFirst , groupSecond bool
222
+ var t * time.Timer
219
223
220
224
inbox := make (chan Message , 8 )
221
225
kbdLost := make (chan bool , 8 )
222
226
kbdLost <- true // init
227
+ if dblKeystroke {
228
+ t = time .NewTimer (time .Duration (dblKeyTimeout ) * time .Millisecond )
229
+ }
223
230
224
231
go scanDevices (inbox , deviceMatch , quietMode , scanOnce )
225
232
@@ -242,9 +249,17 @@ func listenKeyboards(
242
249
case 1 : // key down
243
250
switch ev .Code {
244
251
case keyFirst :
245
- groupFirst = true
252
+ if dblKeystroke {
253
+ t , groupFirst = checkTimeout (t , dblKeyTimeout )
254
+ } else {
255
+ groupFirst = true
256
+ }
246
257
case keySecond :
247
- groupSecond = true
258
+ if dblKeystroke {
259
+ t , groupSecond = checkTimeout (t , dblKeyTimeout )
260
+ } else {
261
+ groupSecond = true
262
+ }
248
263
default : // other keys
249
264
groupFirst = false
250
265
groupSecond = false
@@ -292,3 +307,13 @@ func listenEvents(
292
307
replyTo <- Message {Device : kbd , Events : events }
293
308
}
294
309
}
310
+
311
+ // Checks if the key was pressed before the timer was expired
312
+ // Resets expired timer
313
+ func checkTimeout (t * time.Timer , timeout int ) (* time.Timer , bool ) {
314
+ if t .Stop () {
315
+ return t , true
316
+ }
317
+ t .Reset (time .Duration (timeout ) * time .Millisecond )
318
+ return t , false
319
+ }
0 commit comments