Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/283.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 无需诅咒模块读取箭头键_Python_Python 3.x_Module_Arrow Keys - Fatal编程技术网

Python 无需诅咒模块读取箭头键

Python 无需诅咒模块读取箭头键,python,python-3.x,module,arrow-keys,Python,Python 3.x,Module,Arrow Keys,所以我正在制作一个需要箭头键但不能使用诅咒或chr函数的程序。有没有简单易行的方法 顺便说一句,我已经尝试了inp.key\u UP,但这似乎不起作用。而且我在mac上,所以gtsrc无法工作。我试图。。。 但是,您需要按enter键才能返回值。 祝你好运 import sys def get(): x = sys.stdin.read(1) return x while True: key = get() print key 当用户按某个键时,除非您正在

所以我正在制作一个需要箭头键但不能使用诅咒或chr函数的程序。有没有简单易行的方法

顺便说一句,我已经尝试了inp.key\u UP,但这似乎不起作用。而且我在mac上,所以gtsrc无法工作。

我试图。。。 但是,您需要按enter键才能返回值。 祝你好运

import sys

def get():
    x = sys.stdin.read(1)
    return x
while True: 

    key = get()
    print key

当用户按某个键时,除非您正在以非常低的级别(如Cocoa系统事件挂钩)读取数据,否则终端程序通常会处理该数据,并向您的程序发送一个字节序列,您可以从sys.stdin或/dev/tty读取该字节序列

假设默认设置:

对于ASCII字母、数字等,该字节序列仅为一个字节,带有字符的ord。 对于非ASCII字符,该字节序列是字符的UTF-8编码。在另一个*nix上,你不能指望它是UTF-8,但在macOS上,你可以。 对于箭头之类的特殊键,该序列是一个简单的序列。例如,对于up arrow,您将获得Cursor up\x1b[A,或者Cursor up 1\x1b[1A。您真的不应该假设一个ANSI兼容的终端,您甚至可以将terminal.app配置为发送不同的内容,而不是读取$TERM并查找termcaps条目,但如果您只关心Mac,那么您几乎肯定不会受到影响。 因此,现在您只需将stdin或/dev/tty作为未缓冲的原始二进制文件打开,将其置于原始模式,并确保在程序结束时将其还原,从中读取字节,并识别ANSI控制序列。哦,如果用户的键盘上有任何非ASCII键,它们也将显示为多个字节,您必须对其进行解码s UTF-8

这里有一些东西可以证明这一点:

import termios
import tty

with open('/dev/tty', 'rb', buffering=0) as f:
    stashed = termios.tcgetattr(f.fileno())
    try:
        tty.setraw(f.fileno())
        while True:
            key = f.read(1)
            if key == b'\x03': # Control-C
                break
            if key == b'\x1b': # Escape
                buf = key
                while True:
                    key = f.read(1)
                    buf += key
                    # Cheating a bit; most sequences go until the first letter
                    if key.isalpha():
                        break
                if buf == b'\x1b[A':
                    print('Up arrow!')
                else:
                    print('Some other control sequence:', buf)
            # punting on UTF-8 decoding, treating non-ASCII as control chars
            elif b' ' <= key < b'\x80':
                print(key.decode())
            else:
                print('Some control or non-ASCII key', key)
    finally:
        termios.tcsetattr(f.fileno(), termios.TCSADRAIN, stashed)

如果这不能说明为什么人们通常使用更高级别的库,比如curses,那么就找一些代码来真正正确地处理termcap和locale,而不是假设。

为什么不能使用chr函数呢?我假设限制是老师分配的某个任务的一部分;否则就没有什么意义了。那么,你能吗给我们完整的作业描述?还有,什么是inp.key\u UP?我假设它来自某个第三方库?什么是glsrc?我不能使用chr函数,因为我已经有w,a,s和d作为用户的输入,我不能更改该输入的变量以包括chr,因为这样w,a,s和d就不起作用了,因为它们不是数字。我很抱歉瑞丽,我不明白你说的是什么意思。chr119是“w”。但是箭头键的命令是什么呢