gpt4 book ai didi

python - 如何使用 pyHook 捕获 Shift + PrintScreen?

转载 作者:可可西里 更新时间:2023-11-01 11:26:47 27 4
gpt4 key购买 nike

我试图捕获 Shift+PrintScreen 因为 Ctrl+cthis answer 中被捕获.

虽然答案已经过时,但即使我修复了导入,它也不起作用:

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 键并打开我的屏幕截图应用程序,我能够做到这一点。现在我想捕获 Shift + PrintScreen 并使用其他一些配置打开我的应用程序。如何同时捕获两个 key ?

最佳答案

pyhook source code::HookManager.py列出所有定义的键常量。在您的情况下,您必须结合 event.KeyID VK_SNAPSHOT 检查 Keystate VK_LSHIFT ( 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()

如果您还想将它绑定(bind)到右移键,则必须检查 VK_RSHIFT 键态。

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

关于python - 如何使用 pyHook 捕获 Shift + PrintScreen?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33449264/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com