Skip to content

Instantly share code, notes, and snippets.

@pooladkhay
Created June 14, 2021 05:59
Show Gist options
  • Save pooladkhay/8e0926c14cb75c9a1b1d7b9b5b9dfd3a to your computer and use it in GitHub Desktop.
Save pooladkhay/8e0926c14cb75c9a1b1d7b9b5b9dfd3a to your computer and use it in GitHub Desktop.
Golang - Detect ASCII on keypress
package main
import (
"bufio"
"fmt"
"os"
)
func main() {
fmt.Println("Press ESC button or Ctrl-C to exit this program")
fmt.Println("Press any key to see their ASCII code follow by Enter")
for {
// only read single characters, the rest will be ignored!!
consoleReader := bufio.NewReaderSize(os.Stdin, 1)
fmt.Print(">")
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)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment