在 Golang 和 ncurses 中只接受字母数字

Accepting only alphanumerics in Golang and ncurses

所以,我正在通过使用 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。谢谢!

如果不启用 keypad 模式,(n)curses 将 return 组成特殊键的各个字节。

要修复,请将此添加到程序的初始化中:

stdscr.Keypad(true)   // allow keypad input

这将 return 特殊键,例如右箭头,其值高于 255。goncurses 具有为这些定义的符号,例如 KEY_RIGHT.