Skip to content

Instantly share code, notes, and snippets.

@Jeannot-Muller
Last active November 8, 2022 12:57
Show Gist options
  • Save Jeannot-Muller/dccca5e82c264d7fef5ecd772f7ab3cf to your computer and use it in GitHub Desktop.
Save Jeannot-Muller/dccca5e82c264d7fef5ecd772f7ab3cf to your computer and use it in GitHub Desktop.
read key input on console
reader := bufio.NewReader(os.Stdin)
reader.ReadString('\n')
OR:
package main
import (
"fmt"
"github.com/eiannone/keyboard"
"log"
)
func main() {
err := keyboard.Open()
if err != nil {
log.Fatal(err)
}
defer func() {
_ = keyboard.Close()
}()
fmt.Println("Presse ESC to quit, or any key to print its code.")
for {
char, key, err := keyboard.GetKey()
if err != nil {
log.Fatal(err)
}
if key == keyboard.KeyEsc {
break
}
fmt.Printf("You pressed: rune %q, key %X\r", char, key)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment