在Python中查找箭头键的值:为什么它们是三元组?

New*_*ewb 26 python ascii input control-characters non-ascii-characters

我试图找到我的本地系统分配给箭头键的值,特别是在Python中.我使用以下脚本执行此操作:

import sys,tty,termios
class _Getch:       
    def __call__(self):
            fd = sys.stdin.fileno()
            old_settings = termios.tcgetattr(fd)
            try:
                tty.setraw(sys.stdin.fileno())
                ch = sys.stdin.read(1)
            finally:
                termios.tcsetattr(fd, termios.TCSADRAIN, old_settings)
            return ch

def get():
    inkey = _Getch()
    while(1):
            k=inkey()
            if k!='':break
    print 'you pressed', ord(k)

def main():
    for i in range(0,25):
        get()

if __name__=='__main__':
    main()
Run Code Online (Sandbox Code Playgroud)

然后我运行了脚本,然后点击向上右下方,这给了我这个输出:

$ python getchar.py 
you pressed 27
you pressed 91
you pressed 65
you pressed 27
you pressed 91
you pressed 66
you pressed 27
you pressed 91
you pressed 67
you pressed 27
you pressed 91
you pressed 68
Run Code Online (Sandbox Code Playgroud)

这是异常的,因为它表明箭头键在我的系统上被注册为某种形式的三元组(27-91-6x),因为每次按箭头键都会占用三个get()实例.相比之下,按a,b,c和CTRL-C会给出:

you pressed 97
you pressed 98
you pressed 99
you pressed 3
Run Code Online (Sandbox Code Playgroud)

任何人都可以向我解释为什么我的箭头键的值似乎存储为三元组?为什么会这样?所有平台都是一样的吗?(我正在使用Debian Linux.)如果没有,我应该如何存储箭头键的值?

这里的最终目标是我正在尝试编写一个程序,该程序需要正确识别箭头键并根据按下的箭头键执行功能.

New*_*ewb 34

我想我明白了.

我从这里了解到,每个箭头键都由唯一的ANSI转义码表示.然后我了解到ANSI转义码因系统和应用而异:在我的终端中,cat按下并按下向上箭头给出^[[A,在C中它似乎是\033[A等等.后一部分[A,仍然是相同的,但代码为前面的内容Escape可以是十六进制(以x开头),八进制(以0开头)或十进制(无数字前导).

然后我打开了python控制台,插入了之前收到的三元组,试图找到他们的角色值.事实证明,chr(27)\x1b,chr(91)给了[,并呼吁chr65,66,67,68返回的A,B,C,D分别.然后很明显:\x1b逃脱代码!

然后我注意到ANSI中表示为三元组的箭头键当然表示为三个字符,所以我需要修改我的代码以便一次读取三个字符.结果如下:

import sys,tty,termios
class _Getch:
    def __call__(self):
            fd = sys.stdin.fileno()
            old_settings = termios.tcgetattr(fd)
            try:
                tty.setraw(sys.stdin.fileno())
                ch = sys.stdin.read(3)
            finally:
                termios.tcsetattr(fd, termios.TCSADRAIN, old_settings)
            return ch

def get():
        inkey = _Getch()
        while(1):
                k=inkey()
                if k!='':break
        if k=='\x1b[A':
                print "up"
        elif k=='\x1b[B':
                print "down"
        elif k=='\x1b[C':
                print "right"
        elif k=='\x1b[D':
                print "left"
        else:
                print "not an arrow key!"

def main():
        for i in range(0,20):
                get()

if __name__=='__main__':
        main()
Run Code Online (Sandbox Code Playgroud)


Mau*_*Alo 7

我正在使用 Mac,我使用了以下代码并且它运行良好:我将箭头键的值设为 0、1、2、3(向上、向下、向左、向右):记住 ESC 键的代码 27 总是好的也。此致!

while True:
    key = cv2.waitKey(1) & 0xFF

    # if the 'ESC' key is pressed, Quit
    if key == 27:
        quit()
    if key == 0:
        print "up"
    elif key == 1:
        print "down"
    elif key == 2:
        print "left"
    elif key == 3:
        print "right"
    # 255 is what the console returns when there is no key press...
    elif key != 255:
        print(key)
Run Code Online (Sandbox Code Playgroud)

  • 这对于 openCV 脚本很有用,但如果没有 cv2 窗口,“key”可能会得到无限的“255”。 (2认同)