Last active
November 8, 2022 12:57
-
-
Save Jeannot-Muller/dccca5e82c264d7fef5ecd772f7ab3cf to your computer and use it in GitHub Desktop.
read key input on console
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
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