Python:如何用pyautogui按键激活一个事件?
问题描述:
我已经安装了pyautogui包来使用.hotkey()函数来触发一个事件。例如:如果按下组合键“Ctrl + c”,控制台将显示消息“Hello world”。Python:如何用pyautogui按键激活一个事件?
我想是这样的:
while True:
if pyautogui.hotkey("ctrl", "c"):
print("Hello World")
这是错的,但我知道有没有打印该消息时,我按下Ctrl键和C在同一时间的可能性?
答
我自己解决了这个问题。它似乎是你根本不需要pyautogui模块,你只需要实现tkinter绑定,如下所示:
import tkinter from *
root = TK()
def keyevent(event):
if event.keycode == 67: #Check if pressed key has code 67 (character 'c')
print("Hello World")
root.bind("<Control - Key>", keyevent) #You press Ctrl and a key at the same time
root.mainloop()