-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathkeyevents_linux.go
91 lines (69 loc) · 1.7 KB
/
keyevents_linux.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
// +build linux
package main
import (
"log"
"github.com/BurntSushi/xgbutil"
"github.com/BurntSushi/xgbutil/keybind"
"github.com/BurntSushi/xgbutil/xevent"
)
type keyBinding struct {
Key string
Callback func(string)
XConn *xgbutil.XUtil
Connected bool
}
var keyBindings = make(map[string]*keyBinding)
func startKeybind(name string, key string, callback func(string)) bool {
if keyBind, ok := keyBindings[name]; ok {
if !keyBind.Connected {
newBinding := keyBinding{keyBind.Key, keyBind.Callback, nil, false}
delete(keyBindings, name)
return startKeybind(name, newBinding.Key, newBinding.Callback)
}
return true
}
x, err := xgbutil.NewConn()
if err != nil {
log.Fatal(err)
return false
}
keyBind := keyBinding{key, callback, x, false}
keybind.Initialize(keyBind.XConn)
callbackFunc := keybind.KeyPressFun(
func(X *xgbutil.XUtil, e xevent.KeyPressEvent) {
keyBind.Callback(key)
})
err = callbackFunc.Connect(keyBind.XConn, keyBind.XConn.RootWin(), keyBind.Key, true)
if err != nil {
log.Fatal(err)
return false
}
keyBind.Connected = true
pingBefore, pingAfter, pingQuit := xevent.MainPing(keyBind.XConn)
go func() {
for {
select {
case <-pingBefore:
<-pingAfter
case <-pingQuit:
keyBind.Connected = false
return
}
}
}()
keyBindings[name] = &keyBind
return true
}
func stopKeybind(name string) bool {
if keyBind, ok := keyBindings[name]; ok {
if keyBind.Connected {
keyBind.detachEventsAndDisconnect()
}
}
return true
}
func (keyBind *keyBinding) detachEventsAndDisconnect() {
keybind.Detach(keyBind.XConn, keyBind.XConn.RootWin())
xevent.Detach(keyBind.XConn, keyBind.XConn.RootWin())
xevent.Quit(keyBind.XConn)
}