因此,我通过使用 ncurses 制作一个简单的资源管理游戏来自学一些 Golang。我正在使用 this library将 Golang 连接到 ncurses。
我制作了一个简单的文本输入面板,一次输入一个字符,显示它,然后将它添加到组成用户响应的字符串中。这是它的样子:
// Accept characters, printing them until end
ch := window.GetChar()
kstr := gc.KeyString(ch)
response := ""
cur := 0
for kstr != "enter" {
// Diagnostic print to get key code of current character
window.Move(0,0)
window.ClearToEOL()
window.MovePrint(0, 0, ch)
// If its a backspace or delete, remove a character
// Otherwise as long as its a regular character add it
if ((ch == 127 || ch == 8) && cur != 0){
cur--
response = response[:len(response)-1]
window.MovePrint(y, (x + cur), " ")
} else if (ch >= 33 && ch <= 122 && cur <= 52) {
window.MovePrint(y, (x + cur), kstr)
response = response + kstr
cur++
}
// Get next character
ch = window.GetChar()
kstr = gc.KeyString(ch)
}
但是,箭头键和功能键似乎是作为已经与普通 a-zA-Z 字符相关联的键码出现的。例如,右箭头显示为 67,F1 显示为 80。知道我在这里做错了什么,或者是否有更好的方法通过 ncurses 接收字母数字?我想尽可能避免使用 ncurses 字段和类,因为这里的重点是学习 Golang,而不是 ncurses。谢谢!
最佳答案
如果您不启用键盘模式,(n)curses 将返回构成特殊键的各个字节。
要修复,请将此添加到程序的初始化中:
stdscr.Keypad(true) // allow keypad input
当值高于 255 时,它将返回特殊键,例如右箭头。goncurses 具有为这些键定义的符号,例如 KEY_RIGHT
。
关于go - 在 Golang 和 ncurses 中只接受字母数字,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36679535/