英文:
Scanning for up and down arrow in Go?
问题
我正在尝试在Go中扫描上下箭头键的键码,但似乎不起作用。到目前为止,我尝试了以下代码:
in = bufio.NewReader(os.Stdin)
b, err := in.ReadByte()
fmt.Println("键码:", b, err)
但是当我按下上下箭头键时,它永远不会停止读取(它从不运行Println
语句),而是直接在终端中显示“^[[A”和“^[[B”。有什么想法吗?
英文:
I'm trying to scan for the up and down arrow key codes in Go, but it doesn't seem to be working. So far I've tried this:
in = bufio.NewReader(os.Stdin)
b, err := in.ReadByte()
fmt.Println("Key code:", b, err)
But when I press the up or down key, it never stops reading (it never run the Println
statement) and just displays "^[[A" and "^[[B" directly in the terminal. Any idea?
答案1
得分: 4
我相当确定你不能轻松地获得原始模式输入而不使用操作系统系统调用。请参考https://stackoverflow.com/questions/15159118/read-a-character-from-standard-input-in-go-without-pressing-enter上的答案。
似乎普遍接受的解决方案是使用termbox-go,它显然具有相当好的跨平台能力。
英文:
I'm fairly certain you can't get raw mode input easily without os syscalls. Have a look at the answers to https://stackoverflow.com/questions/15159118/read-a-character-from-standard-input-in-go-without-pressing-enter .
It seems that the generally accepted solution is to use termbox-go which apparently has fairly good cross platform capability.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论