Created
June 14, 2021 05:59
-
-
Save pooladkhay/8e0926c14cb75c9a1b1d7b9b5b9dfd3a to your computer and use it in GitHub Desktop.
Golang - Detect ASCII on keypress
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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