Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/windows/16.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)检测Windows上的箭头键按下_Windows_Python 3.x - Fatal编程技术网

(Python)检测Windows上的箭头键按下

(Python)检测Windows上的箭头键按下,windows,python-3.x,Windows,Python 3.x,我找到了各种各样的方法来检测任何按键,从鼠标点击上的诅咒到创建一个函数来检测按键(还有msvcrt,但有时它必须在Linux上工作),但我总是遇到同样的问题:无论我按下哪个箭头键,所有这些函数都返回b'\xe0'。我在cmd和powershell中进行了尝试,结果相同。我正在运行Win7 Pro 64位 编辑:对不起,我使用了代码并尝试了msvcrt.getch()和click.getchar()我发现一个箭头键由两个字符表示,所以我进行了修改,这样,如果第一个字符被放入,它也会读取第二个字符(

我找到了各种各样的方法来检测任何按键,从鼠标点击上的诅咒到创建一个函数来检测按键(还有msvcrt,但有时它必须在Linux上工作),但我总是遇到同样的问题:无论我按下哪个箭头键,所有这些函数都返回
b'\xe0'
。我在cmd和powershell中进行了尝试,结果相同。我正在运行Win7 Pro 64位


编辑:对不起,我使用了代码并尝试了
msvcrt.getch()
click.getchar()

我发现一个箭头键由两个字符表示,所以我进行了修改,这样,如果第一个字符被放入,它也会读取第二个字符(第三个,wtf Linux?),然后将它们转换成独立于平台的字符串

# Find the right getch() and getKey() implementations
try:
    # POSIX system: Create and return a getch that manipulates the tty
    import termios
    import sys, tty
    def getch():
        fd = sys.stdin.fileno()
        old_settings = termios.tcgetattr(fd)
        try:
            tty.setraw(fd)
            ch = sys.stdin.read(1)
        finally:
            termios.tcsetattr(fd, termios.TCSADRAIN, old_settings)
            return ch

    # Read arrow keys correctly
    def getKey():
        firstChar = getch()
        if firstChar == '\x1b':
            return {"[A": "up", "[B": "down", "[C": "right", "[D": "left"}[getch() + getch()]
        else:
            return firstChar

except ImportError:
    # Non-POSIX: Return msvcrt's (Windows') getch
    from msvcrt import getch

    # Read arrow keys correctly
    def getKey():
        firstChar = getch()
        if firstChar == b'\xe0':
            return {"H": "up", "P": "down", "M": "right", "K": "left"}[getch()]
        else:
            return firstChar

我发现一个箭头键由两个字符表示,所以我修改了它,如果第一个字符被放入,它也会读取第二个字符(第三个是wtf Linux?),然后将它们转换成独立于平台的字符串

# Find the right getch() and getKey() implementations
try:
    # POSIX system: Create and return a getch that manipulates the tty
    import termios
    import sys, tty
    def getch():
        fd = sys.stdin.fileno()
        old_settings = termios.tcgetattr(fd)
        try:
            tty.setraw(fd)
            ch = sys.stdin.read(1)
        finally:
            termios.tcsetattr(fd, termios.TCSADRAIN, old_settings)
            return ch

    # Read arrow keys correctly
    def getKey():
        firstChar = getch()
        if firstChar == '\x1b':
            return {"[A": "up", "[B": "down", "[C": "right", "[D": "left"}[getch() + getch()]
        else:
            return firstChar

except ImportError:
    # Non-POSIX: Return msvcrt's (Windows') getch
    from msvcrt import getch

    # Read arrow keys correctly
    def getKey():
        firstChar = getch()
        if firstChar == b'\xe0':
            return {"H": "up", "P": "down", "M": "right", "K": "left"}[getch()]
        else:
            return firstChar
查看和编辑您的帖子。查看和编辑您的帖子。当读取的第一个字符是
b'\x00'
b'\xe0'
时,它应该只调用第二次。下面是一个在Python 3中处理Unicode字符串的函数;始终获取密钥对(即,前导“\x00”或“\xe0”),以避免将输入缓冲区留在坏状态;并通过返回连接的第一个和第二个字符而不是引发
KeyError
,更优雅地处理未映射的键(例如HOME)。当读取的第一个字符是
b'\x00'
b'\xe0'
时,它应该只调用第二次;始终获取密钥对(即,前导“\x00”或“\xe0”),以避免将输入缓冲区留在坏状态;并通过返回连接的第一个和第二个字符,而不是引发
KeyError
,更优雅地处理未映射的关键帧(例如HOME)。