使用 PyHook3 时为什么有时会出现延迟?

win7 下使用 VS2019 写 python

代码如下, 其实基本上就是 PyHook3 样例给出的东西:

import PyHook3
import pythoncom  # Magic utility that "redirects" to pythoncomxx.dll, only for getPyHookEvent()

def OnMouseEvent(event):
  print('MessageName:',event.MessageName)
  print('Message:',event.Message)
  print('Time:',event.Time)
  print('Window:',event.Window)
  print('WindowName:',event.WindowName)
  print('Position:',event.Position)
  print('Wheel:',event.Wheel)
  print('Injected:',event.Injected)
  print('---')

  # return True to pass the event to other handlers
  # return False to stop the event from propagating
  return True


def getPyHookEvent(mouseEvent = OnMouseEvent):
    # create the hook mananger
    hm = PyHook3.HookManager()


    hm.MouseAllButtonsDown = mouseEvent
    hm.MouseAllButtonsUp = mouseEvent


    hm.HookMouse()


    pythoncom.PumpMessages()

运行, 单击弹出的 cmd 窗口, 下面是输出:

MessageName: mouse left down
Message: 513
Time: -2061365449
Window: 8538218
WindowName: C:\Windows\system32\cmd.exe
Position: (532, 421)
Wheel: 0
Injected: 0
---
MessageName: mouse left up
Message: 514
Time: -2061365293
Window: 8538218
WindowName: 选定 C:\Windows\system32\cmd.exe
Position: (532, 421)
Wheel: 0
Injected: 0
---
MessageName: mouse right up
Message: 517
Time: -2061362438
Window: 8538218
WindowName: C:\Windows\system32\cmd.exe
Position: (495, 485)
Wheel: 0
Injected: 0
---
MessageName: mouse right down
Message: 516
Time: -2061359583
Window: 8538218
WindowName: C:\Windows\system32\cmd.exe
Position: (678, 502)
Wheel: 0
Injected: 0
---
MessageName: mouse right up
Message: 517
Time: -2061359443
Window: 8538218
WindowName: C:\Windows\system32\cmd.exe
Position: (678, 502)
Wheel: 0
Injected: 0

我首先单击一次左键, 立即弹出 mouse left down 消息, 但鼠标动作立即出现明显延迟, 直到单击右键或中键 (如果监听键盘那么键盘输入类似) 才会显示 left upright up , 但 right down 消失了(???).
其他键一切正常, 离开 cmd 窗口单击左键一切正常, 同时也能记录所有点击信息.
请问这种情况可能是怎么发生的? 该怎么避免? 还有人遇到类似的情况吗?

1个回答

weixin_44881875
weixin_44881875 不懂, 我用的是python3, ide也有
7 个月之前 回复
Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问