I am looking for a lightweight and in the best case pure go implementation to capture space without a following enter.
I have seen some people using C as extern in Go or termbox. Is there really no other method of capturing every keyboard stroke?
I have already thought about opening the device directly (in Linux) and trying to read from there.
Any advice of how to do this would be great!
Without more information, it's hard to come up with a perfect example of what you're looking for. However, the basic idea is that you need to switch your terminal into raw mode, where input is passed immediately to your application. x/crypto/ssh/terminal
is a popular library that provides this functionality:
package main
import (
"fmt"
"os"
"golang.org/x/crypto/ssh/terminal"
)
func main() {
oldState, err := terminal.MakeRaw(0)
if err != nil {
panic(err)
}
defer terminal.Restore(0, oldState)
for {
var oneChar [1]byte
_, err := os.Stdin.Read(oneChar[:])
const ETX = '\x03' // ^C
const EOT = '\x04' // ^D
if err != nil || oneChar[0] == ETX || oneChar[0] == EOT {
break
}
if oneChar[0] == ' ' {
fmt.Println("Space pressed!")
break
}
}
}