from pynput.keyboard import Key, Controller
keyboard = Controller()
s_key = input(str(" What leter do you want : "))
keyboard.press(s_key)
keyboard.release(s_key)当我运行这个命令时,它会给我一个错误,但是当我用Key.cmd (对于Windows键)替换s_key时,它可以工作,但是如果我在它要求的地方输入Key.cmd,它就会给我一个错误。我想这是因为,假设我输入了key.cmd,在它要求的地方,它用引号括起来,所以它看起来像这样:
keyboard.press("Key.cmd")
keyboard.release("Key.cmd")我已经看过了,我得出的结论是,当你有一个像这样的变量时,它会用引号将它括起来,我认为pynput.keyboard不会用引号注册特殊的键:
keyboard.press(Key.cmd)
keyboard.release(Key.cmd)发布于 2021-02-17 01:48:14
试试这个:
from pynput.keyboard import Key, Controller
keyboard = Controller()
try:
s_key = getattr(Key, input("What letter do you want : ")) # get the key
except AttributeError:
print("no such key %s " % s_key) # if the key is not valid, notify the user about it
else: # if key is valid
keyboard.press(s_key)
keyboard.release(s_key)您必须键入cmd而不是Key.cmd,因为如果您键入Key.cmd,此代码将尝试使用Key.Key.cmd,这当然不会起作用!
https://stackoverflow.com/questions/66229218
复制相似问题