我正在使用Windows上的python自定義街機啓動器。我想選擇系統和遊戲,然後啓動模擬器 - 並且要求某個組合鍵殺死模擬器。我的所有關鍵掛鉤在隨機應用程序測試時都能正常工作,但是當我真正啓動模擬器(例如Nestopia)時,我的關鍵掛鉤無法啓動。我目前使用RegisterHotKey,它獲取事件,但不是熱鍵。任何人都有一個想法,如何安裝一些足夠低的功能,以便在Nestopia之前真正獲得該事件?這裏是我的代碼:在Windows上,如何安裝最低級別的全局鍵盤鉤子?
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
它不是蟒蛇,但可能這是洞察力,以幫助您搜索:http://stackoverflow.com/questions/1465135/detecting-keyboard-hooks – n611x007 2012-12-19 09:52:10