Python:如何使用pyautogui通过按键激活事件? [英] Python: How to activate an event by keypress with pyautogui?

查看:82
本文介绍了Python:如何使用pyautogui通过按键激活事件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经安装了 pyautogui 包来使用 .hotkey() 函数来触发一个事件.例如:如果您按下组合键Ctrl + c",控制台将显示消息Hello world".

I've installed the pyautogui package to use the .hotkey() function to trigger an event. For example: If you press the key combination "Ctrl + c" the console shall display the message "Hello world".

我尝试过这样的事情:

while True:
   if pyautogui.hotkey("ctrl", "c"):
      print("Hello World")

我知道这是错误的,但是当我同时按下 Ctrl 和 C 时是否有可能打印此消息?

It's wrong I know but is there a possibility to print this message when I've pressed Ctrl and C at the same time?

推荐答案

我自己解决了这个问题.似乎您根本不​​需要 pyautogui 模块,您只需要像这样实现 tkinter 绑定:

I solved the problem myself. It seems to be you don't need the pyautogui modul at all and you only have to implement tkinter bindings like this:

from tkinter import *

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()

这篇关于Python:如何使用pyautogui通过按键激活事件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆