Future Refference
- published
- reading time
- 1 minute
I am just putting this piece of code here for future reference because I keep needing to rewrite or Google for it.
It is a “simple” way to get terminal key presses in Linux-based systems.
package main
import (
"fmt"
"os"
"os/exec"
)
func main() {
exec.Command("stty", "-F", "/dev/tty", "cbreak", "min", "1").Run()
exec.Command("stty", "-F", "/dev/tty", "-echo").Run()
var b []byte = make([]byte, 1)
for {
os.Stdin.Read(b)
fmt.Println("I got the byte", b, "("+string(b)+")")
}
}