Skip to content

Instantly share code, notes, and snippets.

@goh-chunlin
Last active October 31, 2020 04:26
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save goh-chunlin/cf8a20db4ac1e3b6ba418386b2e6997f to your computer and use it in GitHub Desktop.
Save goh-chunlin/cf8a20db4ac1e3b6ba418386b2e6997f to your computer and use it in GitHub Desktop.
Reading and parsing event messages in /dev/input/event1 on 32-bit platform with Golang.
package main
import (
"bytes"
"encoding/binary"
"fmt"
"os"
"time"
)
func main() {
f, err := os.Open("/dev/input/event1")
if err != nil {
panic(err)
}
defer f.Close()
b := make([]byte, 16)
for {
f.Read(b)
fmt.Printf("%b\n", b)
sec := binary.LittleEndian.Uint32(b[0:8])
t := time.Unix(int64(sec), 0)
fmt.Println(t)
var value int32
typ := binary.LittleEndian.Uint16(b[8:10])
code := binary.LittleEndian.Uint16(b[10:12])
binary.Read(bytes.NewReader(b[12:]), binary.LittleEndian, &value)
fmt.Printf("type: %x\ncode: %d\nvalue: %d\n", typ, code, value)
}
}
@goh-chunlin
Copy link
Author

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment