Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/dart/3.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
如何查看pynput python 3.7中按下的字符?_Python_Python 3.x_Pynput - Fatal编程技术网

如何查看pynput python 3.7中按下的字符?

如何查看pynput python 3.7中按下的字符?,python,python-3.x,pynput,Python,Python 3.x,Pynput,您如何看到pynput中按下了哪个按钮 from pynput.keyboard import Key, Listener def a(key): print('{0} pressed'.format( key)) if key == 'a': print('ape') with Listener(on_press = a) as listener: listener.join() 看不见工作 from pynput.keyboard

您如何看到pynput中按下了哪个按钮

from pynput.keyboard import Key, Listener

def a(key):
    print('{0} pressed'.format(
        key))
    if key == 'a':
        print('ape')

with Listener(on_press = a) as listener:
    listener.join()
看不见工作

from pynput.keyboard import Listener
def a(key):
    print(f"{key}, was pressed")
    if key.char == "a":
        print("foo")


with Listener(on_press=a) as listener:
    listener.join()
输出:

q'q', was pressed
w'w', was pressed
a'a', was pressed
foo
s's', was pressed
但是,如果您按下caps lock(大写锁定),这将导致问题,例如,我将在try except块中放置:

try:
    if key.char == 'a':
        print("foo")
except AttributeError:
    pass

@塞巴斯蒂安,我已经更新了答案。我想这正是你想要的。