如何用pyHook捕获Shift + PrintScreen?

我试图捕获Shift + PrintScreen作为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,并用其他configuration打开我的应用程序。 我怎样才能同时捕获两个关键?

Solutions Collecting From Web of "如何用pyHook捕获Shift + PrintScreen?"

pyhook源代码:: HookManager.py列出了所有定义的键常量。 在你的情况下,你将不得不检查Keystate VK_LSHIFT结合event.KeyID VK_SNAPSHOT (PrintScrn键)。 这是一个工作的例子:

 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键状态。

 if (GetKeyState(HookConstants.VKeyToID('VK_LSHIFT')) or GetKeyState(HookConstants.VKeyToID('VK_RSHIFT'))) and event.KeyID == HookConstants.VKeyToID('VK_SNAPSHOT'):