-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
86 lines (70 loc) · 1.72 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
package main
import (
"fmt"
"os"
"sync"
"github.com/go-vgo/robotgo"
"github.com/veandco/go-sdl2/sdl"
)
// DOCS: Keys can be found at:
// https://github.com/go-vgo/robotgo/blob/master/docs/keys.md
func pressKey(key string, group *sync.WaitGroup) {
group.Add(1)
robotgo.KeyDown(key)
robotgo.KeyUp(key)
group.Done()
}
func listenXboxJoyStick() error {
os.Setenv("SDL_JOYSTICK_ALLOW_BACKGROUND_EVENTS", "1")
var (
joysticks []*sdl.Joystick
group sync.WaitGroup
)
err := sdl.Init(sdl.INIT_EVERYTHING)
if err != nil {
return err
}
defer sdl.Quit()
sdl.JoystickEventState(sdl.ENABLE)
fmt.Println("Capturing joystick events...")
const (
XboxGuideButton = 10
key = "insert"
)
running := true
for running {
for event := sdl.PollEvent(); event != nil; event = sdl.PollEvent() {
switch polledEvent := event.(type) {
case *sdl.QuitEvent:
running = false
case *sdl.JoyButtonEvent:
if polledEvent.State == sdl.PRESSED && polledEvent.Button == XboxGuideButton {
go pressKey(key, &group)
fmt.Printf("'%s' was pressed!\n", key)
}
case *sdl.JoyDeviceAddedEvent:
joystick := sdl.JoystickOpen(int(polledEvent.Which))
if joystick != nil {
joysticks = append(joysticks, joystick)
fmt.Println("Joystick", polledEvent.Which, "connected")
}
case *sdl.JoyDeviceRemovedEvent:
joystick := joysticks[int(polledEvent.Which)]
if joystick != nil {
joystick.Close()
}
fmt.Println("Joystick", polledEvent.Which, "disconnected")
}
}
// Waiting 10 milliseconds before polling the events again
sdl.Delay(10)
}
group.Wait()
return err
}
func main() {
errorWhileExecuting := listenXboxJoyStick()
if errorWhileExecuting != nil {
os.Exit(1)
}
}