在Go中扫描上下箭头?

huangapple go评论84阅读模式
英文:

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.

huangapple
  • 本文由 发表于 2013年7月30日 09:38:58
  • 转载请务必保留本文链接:https://go.coder-hub.com/17937000.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定