Pynput 键盘
Pynput keyboard
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
替换 s_key
时(对于 windows 键)它有效但是如果我在它询问的地方输入 Key.cmd
给我一个错误。我认为这是因为假设我输入了 key.cmd
它要求它用引号括起来所以它看起来像这样:
keyboard.press("Key.cmd")
keyboard.release("Key.cmd")
我已经看过了,我得出的结论是,当你有一个像这样的变量时,它会用引号引起来,我认为 pynput.keyboard
不会在引号中注册特殊键:
keyboard.press(Key.cmd)
keyboard.release(Key.cmd)
试试这个:
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
,当然,不会工作!
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
替换 s_key
时(对于 windows 键)它有效但是如果我在它询问的地方输入 Key.cmd
给我一个错误。我认为这是因为假设我输入了 key.cmd
它要求它用引号括起来所以它看起来像这样:
keyboard.press("Key.cmd")
keyboard.release("Key.cmd")
我已经看过了,我得出的结论是,当你有一个像这样的变量时,它会用引号引起来,我认为 pynput.keyboard
不会在引号中注册特殊键:
keyboard.press(Key.cmd)
keyboard.release(Key.cmd)
试试这个:
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
,当然,不会工作!