Last active
October 31, 2020 04:26
-
-
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This is based on Janczer's solution for 64-bit platforms.