我正在尝试找到一种方法来检测按键,然后根据按键运行一个方法。
我已经可以使用 Tkinter 做到这一点。但我不能做的是在窗口处于后台时检测按键。当我玩游戏时,我将在后台运行这个程序。我需要它能够在我玩游戏时检测输入。
有什么办法可以用 Tkinter 或其他东西来做到这一点吗?最好我不想下载任何外部的东西,因为我想将其分发给其他人。
pyHook http://sourceforge.net/projects/pyhook/似乎它对此很有效(由furas提到)
from pyHook import HookManager
from win32gui import PumpMessages, PostQuitMessage
class Keystroke_Watcher(object):
def __init__(self):
self.hm = HookManager()
self.hm.KeyDown = self.on_keyboard_event
self.hm.HookKeyboard()
def on_keyboard_event(self, event):
try:
if event.KeyID == keycode_youre_looking_for:
self.your_method()
finally:
return True
def your_method(self):
pass
def shutdown(self):
PostQuitMessage(0)
self.hm.UnhookKeyboard()
watcher = Keystroke_Watcher()
PumpMessages()
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)