我在Windows上用python开发一个自定义的arcade启动器。我想选择系统和游戏,然后启动模拟器-并需要一个特定的组合键来杀死模拟器。我所有的键钩子在测试随机应用程序时都能工作,但是当我真正启动模拟器(例如Nestopia)时,我的键钩子无法启动。我当前使用的是RegisterHotKey,它获取事件,但不获取热键。有人知道如何在Nestopia之前安装一个足够低的设备来实际获取事件吗?我的代码是:
import ctypes
import win32con
from ctypes import wintypes
from ctypes import byref
user32 = ctypes.windll.user32
class SimplekeyboardHook:
def getNextId(self):
SimpleKeyboardHook._id += 1
return SimpleKeyboardHook._id
# modifiers is a bitmask with win32con.[MOD_SHIFT, MOD_ALT, MOD_CONTROL, MOD_WIN]
def waitFor(self, key, modifiers):
# coerce to 0 if necessary
modifiers = modifiers or 0
id = self.getNextId()
hk = user32.RegisterHotKey(None, id, modifiers, key)
print "register hotkey: ",hk
if not hk:
print "Unable to register hotkey for key ", key
return False
print "registered id", id
try:
msg = wintypes.MSG()
while user32.GetMessageA(byref(msg), None, 0, 0) != 0:
print "got message",msg.message,"which is not",win32con.WM_HOTKEY
if msg.message == win32con.WM_HOTKEY:
print "got hotkey"
if msg.wParam == id:
print "found proper hotkey"
return True
user32.TranslateMessage(byref(msg))
user32.DispatchMessageA(byref(msg))
finally:
user32.UnregisterHotKey(None, id)
return False
SimpleKeyboardHook._id = 0
你应该看看user32的SetWindowsHookEx。这些函数允许您注册全局键盘挂钩。(别忘了打电话给CallNextHookEx把它们传下去。)
链接:http://msdn.microsoft.com/en-us/library/ms644990(v=vs.85).aspx
不过,我不知道如何在python中做到这一点,抱歉。在
您尝试过在SourceForge上使用pyHook吗?您可以检查DaniWeb的示例用法。在
相关问题 更多 >
编程相关推荐