2010-06-15 102 views
4

我想在python中使用pyhook創建一個全局熱鍵,它應該只能用alt鍵來工作。幫助pyHook錯誤

這裏是源:

import pyHook 
import pythoncom 

hm = pyHook.HookManager() 

def OnKeyboardEvent(event): 
    if event.Alt == 32 and event.KeyID == 49: 
     print 'HERE WILL BE THE CODE' 

hm.KeyDown = OnKeyboardEvent 
hm.HookKeyboard() 
pythoncom.PumpMessages() 

但是當我執行,只與第二鍵(數字1 = 49)的第二次按工程...,讓這個錯誤:

http://img580.imageshack.us/img580/1858/errord.png

我該如何解決?在第一次按下的時間工作。從您在您的處理程序結束需要一個返回值tutorial

回答

8

注:

def OnKeyboardEvent(event): 
    if event.Alt == 32 and event.KeyID == 49: 
     print 'HERE WILL BE THE CODE' 

    # return True to pass the event to other handlers 
    return True 

我同意這是一個從不管是需要的文檔曖昧,但你需要返回true或false(或可能是任何整數值),用任何「假」值(例如0)阻塞事件,使得沒有後續處理程序獲取它。 (這使您可以吞下某些鍵條件,如在本教程的事件過濾部分。)

(這是不容易搞清楚,因爲它看起來!:-))

+0

謝謝你,工作完美... =) – Shady 2010-06-16 02:58:33