Premise:
I know there are a handful of questions touching on this, but after looking through them (and implementing every answer), I'm at a bit of a loss.
I have a binary (showkey
), that I'd like to process the output of in realtime, in order to let a secondary system know when a key has been both pressed, and released.
Setup:
- Raspberry pi
- Physical GPIO connection to a digital I/O system on the secondary hardware
- Keyboard plugged into Rpi
- Golang code on Rpi to monitor key actuation and release, and switch a GPIO pin up/down
Code:
package main
import (
"fmt"
"os/exec"
"bufio"
)
func main() {
fmt.Println("Tracking Input!")
cmd := exec.Command("/usr/bin/showkey", "-k")
pipe, err := cmd.StdoutPipe()
if err != nil {
panic(err)
}
err = cmd.Start()
if err != nil {
panic(err)
}
scanner := bufio.NewScanner(pipe)
for scanner.Scan() {
m := scanner.Text()
fmt.Println(m)
}
err = cmd.Wait()
if err != nil {
panic(err)
}
}
Issue:
The current code (and every other iteration I've tried after reading through every other question posted) does not in fact allow golang to process the data. the fmt.Println(m)
prints out after the showkey binary ends (10 seconds after last key press).
I've confirmed through the showkey source code that it prints to stdout, and am really at a loss here. The fmt.Println("Tracking Inputs!")
statement comes through, and then nothing until showkey stops (though showkey does in fact track the key presses as it's supposed to), when everything is printed at once.
Any thoughts?