我使用pyhook
和pyhk
來映射Windows XP機器上的擊鍵,並且除了在應用程序中已經存在擊鍵(例如,ctrl + z)時它工作正常。在這種情況下,ctrl + z會傳遞給應用程序,並且會觸發已映射到該應用程序的操作。從生成的擊鍵中阻止pyhook?
如果您熟悉autohotkey
,請注意autohotkey
通過定義可以選擇傳遞給底層應用程序的熱鍵來解決此問題。這個想法有一些代碼。請注意,我試圖跟蹤ctrl鍵何時關閉。
import pythoncom, pyHook
control_down = False
def OnKeyboardEvent_up(event):
global control_down
if event.Key=='Lcontrol' or event.Key=='Rcontrol':
control_down=False
return True
def OnKeyboardEvent(event,action=None,key='Z',context=None):
global control_down
if event.Key=='Lcontrol' or event.Key=='Rcontrol':
control_down=True
if control_down and event.Key==key:
print 'do something'
return False
if event.Key=='Pause':
win32gui.PostQuitMessage(1)
return False
# return True to pass the event to other handlers
return True
if __name__ == '__main__':
hm = pyHook.HookManager()
hm.KeyDown = OnKeyboardEvent
hm.KeyUp = OnKeyboardEvent_up
hm.HookKeyboard() # set the hook
pythoncom.PumpMessages() # wait forever
任何幫助表示讚賞。
謝謝!
可悲的是(跨平臺)熱鍵支持是__very bad__現在Python編寫的。像這樣的_Advanced(咳嗽)_東西是很困難的。我正在考慮寫我自己這段時間很快就會出現。 – orlp 2012-07-25 21:11:50