Reputation: 51
I want to be able to tell if a user presses things like Ctrl+[something] or Esc on their keyboard in Go. The program runs in a Linux environment inside a terminal.
Upvotes: 5
Views: 5694
Reputation: 2517
in := bufio.NewScanner(os.Stdin)
data := in.Bytes()
signal.Notify(os.Interrupt, os.Kill) // os.Interrupt=0x2 os.Kill=0x9
import (
"fmt"
term "github.com/nsf/termbox-go"
)
func main() {
err := term.Init()
if err != nil {
panic(err)
}
defer term.Close()
for {
switch ev := term.PollEvent(); ev.Type {
case term.EventKey:
switch ev.Key {
case term.KeyEsc:
term.Sync()
fmt.Println("ESC pressed")
case term.KeyF1:
term.Sync()
fmt.Println("F1 pressed")
case term.KeyInsert:
term.Sync()
fmt.Println("Insert pressed")
case term.KeyDelete:
term.Sync()
fmt.Println("Delete pressed")
case term.KeyHome:
term.Sync()
fmt.Println("Home pressed")
case term.KeyEnd:
term.Sync()
fmt.Println("End pressed")
case term.KeyPgup:
term.Sync()
case term.KeyArrowRight:
term.Sync()
fmt.Println("Arrow Right pressed")
case term.KeySpace:
term.Sync()
fmt.Println("Space pressed")
case term.KeyBackspace:
term.Sync()
fmt.Println("Backspace pressed")
case term.KeyEnter:
term.Sync()
fmt.Println("Enter pressed")
case term.KeyTab:
term.Sync()
fmt.Println("Tab pressed")
default:
term.Sync()
fmt.Println("ASCII : ", ev.Ch)
}
case term.EventError:
panic(ev.Err)
}
}
}
consoleReader := bufio.NewReaderSize(os.Stdin, 1)
input, _ := consoleReader.ReadByte()
ascii := input
// ESC = 27 and Ctrl-C = 3
if ascii == 27 || ascii == 3 {
fmt.Println("Exiting...")
os.Exit(0)
}
fmt.Println("ASCII : ", ascii)
Upvotes: 9