2010-01-20 70 views
0

我有一個Python程序,使用SendKeys將擊鍵發送到另一個應用程序。但是,某些鍵擊必須在進行一些處理後纔會發送到應用程序(這需要一段未知時間)。到目前爲止,我不得不讓Python應用程序知道處理已經通過Alt + Tabbing返回到DOS窗口並按Enter完成。我想要一個按鍵組合(Shift + F1或類似的東西),我可以在接收應用程序中點擊,這個程序可以讓Python程序繼續運行,而不必切換回DOS窗口。我該如何做到這一點,所以即使焦點位於另一個窗口上,我也可以在Python中檢測擊鍵?Python檢測到發送到另一個應用程序的擊鍵

回答

0

看一看pyHook

它可以讓鍵盤鉤子:

import pythoncom, pyHook 

def OnKeyboardEvent(event): 
    print 'MessageName:',event.MessageName 
    print 'Message:',event.Message 
    print 'Time:',event.Time 
    print 'Window:',event.Window 
    print 'WindowName:',event.WindowName 
    print 'Ascii:', event.Ascii, chr(event.Ascii) 
    print 'Key:', event.Key 
    print 'KeyID:', event.KeyID 
    print 'ScanCode:', event.ScanCode 
    print 'Extended:', event.Extended 
    print 'Injected:', event.Injected 
    print 'Alt', event.Alt 
    print 'Transition', event.Transition 
    print '---' 

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

# create a hook manager 
hm = pyHook.HookManager() 
# watch for all mouse events 
hm.KeyDown = OnKeyboardEvent 
# set the hook 
hm.HookKeyboard() 
# wait forever 
pythoncom.PumpMessages()