1

我试图捕获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 keyboard events
hm.KeyDown = OnKeyboardEvent
# set the hook
hm.HookKeyboard()
# wait forever
pythoncom.PumpMessages()

我想捕获 PrintScreen 键并打开我能够做到的 Screenshot 应用程序。现在我想捕获 Shift + PrintScreen 并使用其他一些配置打开我的应用程序。如何一次捕获两个密钥?

4

1 回答 1

4

pyhook 源代码::HookManager.py列出了所有定义的键常量。在您的情况下,您必须检查Keystate VK_LSHIFTevent.KeyID VK_SNAPSHOT(PrintScrn Key) 的组合。这是一个工作示例:

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'):
于 2015-11-19T19:59:33.067 回答