Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/python-2.7/5.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(v2.7)中是否有任何关键侦听器_Python_Python 2.7 - Fatal编程技术网

python(v2.7)中是否有任何关键侦听器

python(v2.7)中是否有任何关键侦听器,python,python-2.7,Python,Python 2.7,我需要在下面的伪代码中执行类似的操作。(键侦听器) 我使用“win.getkey()”尝试了这种程序,但似乎无法正常工作。如何用python编写适当的密钥侦听器?(没有第三方库)您可以尝试以下代码。它不包含第三方模块 代码: import sys, tty, os, termios def getkey(): old_settings = termios.tcgetattr(sys.stdin) tty.setcbreak(sys.stdin.fileno()) tr

我需要在下面的伪代码中执行类似的操作。(键侦听器)


我使用“win.getkey()”尝试了这种程序,但似乎无法正常工作。如何用python编写适当的密钥侦听器?(没有第三方库)

您可以尝试以下代码。它不包含第三方模块

代码:

import sys, tty, os, termios


def getkey():
    old_settings = termios.tcgetattr(sys.stdin)
    tty.setcbreak(sys.stdin.fileno())
    try:
        while True:
            b = os.read(sys.stdin.fileno(), 3).decode()
            if len(b) == 3:
                k = ord(b[2])
            else:
                k = ord(b)
            key_mapping = {
                127: 'backspace',
                10: 'return',
                32: 'space',
                9: 'tab',
                27: 'esc',
                65: 'up',
                66: 'down',
                67: 'right',
                68: 'left'
            }
            return key_mapping.get(k, chr(k))
    finally:
        termios.tcsetattr(sys.stdin, termios.TCSADRAIN, old_settings)


try:
    while True:
        k = getkey()
        print("Detected key: {}".format(k))
        if k == 'esc':
            quit()
        else:
            print(k)
except (KeyboardInterrupt, SystemExit):
    os.system('stty sane')
    print('stopping.')
>>> python3 test.py 
Detected key: a
a
Detected key: s
s
Detected key: d
d
Detected key: f
f
Detected key: right
right
Detected key: left
left
Detected key: space
space
Detected key: tab
tab
stopping.
测试和输出:

import sys, tty, os, termios


def getkey():
    old_settings = termios.tcgetattr(sys.stdin)
    tty.setcbreak(sys.stdin.fileno())
    try:
        while True:
            b = os.read(sys.stdin.fileno(), 3).decode()
            if len(b) == 3:
                k = ord(b[2])
            else:
                k = ord(b)
            key_mapping = {
                127: 'backspace',
                10: 'return',
                32: 'space',
                9: 'tab',
                27: 'esc',
                65: 'up',
                66: 'down',
                67: 'right',
                68: 'left'
            }
            return key_mapping.get(k, chr(k))
    finally:
        termios.tcsetattr(sys.stdin, termios.TCSADRAIN, old_settings)


try:
    while True:
        k = getkey()
        print("Detected key: {}".format(k))
        if k == 'esc':
            quit()
        else:
            print(k)
except (KeyboardInterrupt, SystemExit):
    os.system('stty sane')
    print('stopping.')
>>> python3 test.py 
Detected key: a
a
Detected key: s
s
Detected key: d
d
Detected key: f
f
Detected key: right
right
Detected key: left
left
Detected key: space
space
Detected key: tab
tab
stopping.

注意:如果您能够使用外部Python模块,我建议使用
pynput
键盘
Python模块。链接:,

您对win.getkey()有什么问题?使用的{key=win.getkey(),win.clear(),win.addstr(“检测到的键:”),win.addstr(str(键))},'检测到的键:'未显示,并被卡住。