Read a character from standard input in Go (without pressing Enter)

前端 未结 6 1693
甜味超标
甜味超标 2020-12-08 20:29

I want to my app shows:

press any key to exit ...

And when I pressed any key, it exits.

How can I achieve this?

6条回答
  •  轻奢々
    轻奢々 (楼主)
    2020-12-08 20:58

    You could use this library (mine): https://github.com/eiannone/keyboard

    This is an example for getting a single keystroke:

    char, _, err := keyboard.GetSingleKey()
    if (err != nil) {
        panic(err)
    }
    fmt.Printf("You pressed: %q\r\n", char)
    

提交回复
热议问题