【问题标题】:Accepting only alphanumerics in Golang and ncurses仅接受 Golang 和 ncurses 中的字母数字
【发布时间】:2016-04-17 17:02:51
【问题描述】:

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

【问题讨论】:

    标签: go ncurses


    【解决方案1】:

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

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

    stdscr.Keypad(true)   // allow keypad input
    

    它将返回特殊键,例如右箭头作为大于 255 的值。goncurses 具有为这些定义的符号,例如,KEY_RIGHT

    【讨论】:

    • 是的,就是这样。非常感谢!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-08-13
    • 1970-01-01
    • 2014-06-26
    • 2021-06-15
    相关资源
    最近更新 更多