1

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?

blackgreen
  • 34,072
  • 23
  • 111
  • 129
Xarus
  • 6,589
  • 4
  • 15
  • 22
  • 1
    How does showkey reads keys? From the terminal? If you exec it like this, it won't have a terminal attached. – Burak Serdar Nov 28 '22 at 22:45
  • 1
    Also be aware that you are forcing the output to be line buffered by the fact that you are reading the output by scanning for lines. – JimB Nov 28 '22 at 23:03
  • 1
    Some commands buffer output when stdout is not a terminal. If showkeys is one of those commands, then [this question](https://stackoverflow.com/q/3465619/5728991) may help. – Charlie Tumahai Nov 28 '22 at 23:09
  • take a look at https://tinygo.org to manage RPi hardware , it's super easy working with gpio – wakumaku Nov 28 '22 at 23:20
  • And combine a library like this one to listen for keystrokes https://github.com/eiannone/keyboard – wakumaku Nov 28 '22 at 23:27
  • @BurakSerdar it uses IOCTL to read, and outputs directly to stdout for press and release – Xarus Nov 29 '22 at 01:49
  • @JimB yeah, that's fine - the purpose of this is to get the press and release time which will be used in conjunction with a robotic arm pressing the key (very slowly) – Xarus Nov 29 '22 at 01:53
  • @wakumaku very familiar with RPi+GPIO stuff (I cut out my GPIO code from this for clarity), and that library doesn't provide release events which I need – Xarus Nov 29 '22 at 01:54
  • @CeriseLimón You're a legend! That was in fact the issue, and using stdbuf as per one of the answers has easily resolved the situation. If you want to throw an answer down I'll mark it correct :) – Xarus Nov 29 '22 at 16:54
  • Are you aware that this question was closed as duplicate by Cerise Limon themselves with the very link in their comment? So the remark you added to your question to have it reopened seems contradictory. I rolled back your edit on the grounds that you confirmed you were able to find a solution with Cerise's link. – blackgreen Nov 30 '22 at 14:28

0 Answers0