我使用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