2011-06-14 73 views
6

我正在使用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 
+0

它不是蟒蛇,但可能这是洞察力,以帮助您搜索:http://stackoverflow.com/questions/1465135/detecting-keyboard-hooks – n611x007 2012-12-19 09:52:10

回答

1

你一定要看看user32的SetWindowsHookEx。这些功能允许您注册全局键盘挂钩。 (不要忘记通过调用CallNextHookEx方法来传递它们。)

链接:http://msdn.microsoft.com/en-us/library/ms644990(v=vs.85).aspx

我不知道该怎么做,从蟒蛇虽然,对不起。

+0

我试过,以及 - 它似乎有同样的问题作为热键。 :( – Joel 2011-06-14 18:41:45

0

您是否试过在SourceForge上使用pyHook呢?举例来说,你可以检查DaniWeb