我试图捕获 Shift + PrintScreen ,因为在{{3}中捕获了 Ctrl + c }。
虽然答案已经过时,但即使我修复了导入,它也不起作用:
import pythoncom
from pyHook import HookManager, GetKeyState, HookConstants
def OnKeyboardEvent(event):
ctrl_pressed = GetKeyState(HookConstants.VKeyToID('VK_CONTROL') >> 15)
if ctrl_pressed and HookConstant.IDToName(event.keyId) == 'd':
print("ctrl plus d was pressed at same time")
return True
# create a hook manager
hm = HookManager()
# watch for all mouse events
hm.KeyDown = OnKeyboardEvent
# set the hook
hm.HookKeyboard()
# wait forever
pythoncom.PumpMessages()
我想捕获PrintScreen键并打开我的截屏应用程序,我可以这样做。现在我想捕获Shift + PrintScreen并使用其他配置打开我的应用程序。如何一次捕获两个密钥?
答案 0 :(得分:2)
pyhook source code::HookManager.py列出了所有已定义的键常量。在您的情况下,您必须结合Keystate
VK_LSHIFT
(PrintScrn键)检查event.KeyID
VK_SNAPSHOT
。这是一个有效的例子:
import pythoncom
from pyHook import HookManager, GetKeyState, HookConstants
def OnKeyboardEvent(event):
# in case you want to debug: uncomment next line
# print repr(event), event.KeyID, HookConstants.IDToName(event.KeyID), event.ScanCode , event.Ascii, event.flags
if GetKeyState(HookConstants.VKeyToID('VK_LSHIFT')) and event.KeyID == HookConstants.VKeyToID('VK_SNAPSHOT'):
print("shift + snapshot pressed")
elif GetKeyState(HookConstants.VKeyToID('VK_CONTROL')) and HookConstants.IDToName(event.KeyID) == 'D':
print("ctrl + d pressed")
return True
# create a hook manager
hm = HookManager()
# watch for all mouse events
hm.KeyDown = OnKeyboardEvent
# set the hook
hm.HookKeyboard()
# wait forever
pythoncom.PumpMessages()
如果你想将它绑定到右移键,你必须检查VK_RSHIFT
keystate。
if (GetKeyState(HookConstants.VKeyToID('VK_LSHIFT')) or GetKeyState(HookConstants.VKeyToID('VK_RSHIFT'))) and event.KeyID == HookConstants.VKeyToID('VK_SNAPSHOT'):