-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
73 lines (63 loc) · 1.29 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
package main
import (
"fmt"
"os"
"github.com/gvalkov/golang-evdev"
"net/http"
"net/url"
)
const eventURL = "http://10.0.1.100:1880/huis"
func main() {
if len(os.Args) <= 1 {
fmt.Println("You must specify device name.")
os.Exit(1)
}
dev, err := evdev.Open(os.Args[1])
if err != nil {
fmt.Printf("Unable to open input device: %s\n", os.Args[1])
os.Exit(1)
}
fmt.Printf("Listening for evens of %s.\n", os.Args[1])
for {
events, err := dev.Read()
if err != nil {
fmt.Printf("Can not read device envets: %v\n", err)
continue
}
for i := range events {
str, err := detectDownKeyEvent(&events[i])
if err != nil {
continue
}
fmt.Println(str)
if err := sendKey(str); err != nil {
fmt.Printf("Can not send key: %v\n", err)
}
}
}
}
func sendKey(key string) error {
values := url.Values{}
values.Add("key", key)
_, err := http.PostForm(eventURL, values)
return err
}
func detectDownKeyEvent(ev *evdev.InputEvent) (string, error) {
if ev.Value != 1 {
return "", fmt.Errorf("Value is not 1: %d", ev.Value)
}
var codeName string
code := int(ev.Code)
val, hashkey := evdev.KEY[code]
if hashkey {
codeName = val
} else {
val, hashkey := evdev.BTN[code]
if hashkey {
codeName = val
} else {
codeName = "?"
}
}
return codeName, nil
}