-
Notifications
You must be signed in to change notification settings - Fork 0
/
usage_test.go
48 lines (40 loc) · 868 Bytes
/
usage_test.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
package input_test
import (
"context"
"io"
"log"
"os"
"path/filepath"
"time"
"github.com/duncanvanzyl/input-event-processor"
)
func printEvent(e *input.Event) {
log.Printf("got event: %v", e)
}
func Example() {
// create processor
ip := input.NewProcessor()
// register specific event
motion := input.TypeCode{
Type: input.EV_KEY,
Code: input.KEY_SPACE,
}
ip.Register(motion, printEvent)
// register all events of a type
dial := input.TypeCode{
Type: input.EV_REL,
Code: input.REL_CNT,
}
ip.Register(dial, printEvent)
// open event devices
fns, _ := filepath.Glob("/dev/input/event*")
var fs []io.ReadCloser
for _, fn := range fns {
f, _ := os.Open(fn)
fs = append(fs, f)
}
// Run Watch and Process
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()
_ = ip.WatchAndProcess(ctx, fs)
}