teamplayer 和 pyhook 交互奇怪

发布于 2024-09-11 16:51:24 字数 1876 浏览 1 评论 0原文

我正在使用 teamplayer,它可以让您将更多鼠标连接到计算机以同时使用。我还使用 pyHook 来捕获鼠标事件,代码如下:

import pyHook
import pythoncom

def onclick(event):
  # called when mouse events are received
  print 'MessageName:',event.MessageName
  print 'Message:',event.Message
  print 'Time:',event.Time
  print 'WindowName:',event.WindowName
  print 'Position:',event.Position
  print '---'
  return True

hm = pyHook.HookManager()
hm.MouseLeftDown = onclick
hm.MouseLeftUp = onclick
hm.HookMouse()
pythoncom.PumpMessages()

该代码在没有 teamplayer 的情况下也可以正常工作 - 它可以准确地检测鼠标按钮的按下和向上。如果我在程序运行时启动 teamplayer,那么它会继续正常工作,这次可以准确地检测两只鼠标的点击。

但是,如果我在 teamplayer 启动之后启动程序,那么每次鼠标点击都会变成两次:

MessageName: mouse left down
Message: 513
Time: 7231317
WindowName: None
Position: (673, 367)
---
MessageName: mouse left down
Message: 513
Time: 7231317
WindowName: None
Position: (673, 367)
---
MessageName: mouse left up
Message: 514
Time: 7231379
WindowName: None
Position: (673, 367)
---
MessageName: mouse left up
Message: 514
Time: 7231379
WindowName: None
Position: (673, 367)

这没问题 - 我可以检测到具有相同时间戳的点击并忽略第二次。但是,当我用不同的鼠标单击时,模式很奇怪:

MessageName: mouse left down
Message: 513
Time: 7305916
WindowName: C:\Python25\python.exe
Position: (569, 306)
---
MessageName: mouse left down
Message: 513
Time: 7305916
WindowName: C:\Python25\python.exe
Position: (722, 365)
---
MessageName: mouse left up
Message: 514
Time: 7309598
WindowName: C:\Python25\python.exe
Position: (722, 365)
---
MessageName: mouse left up
Message: 514
Time: 7309598
WindowName: C:\Python25\python.exe
Position: (722, 365)

也就是说,第一个向下事件使用最后一个向上事件的坐标!问题还在于,错误的事件是第一个,这使得检测正确的事件变得更加困难(我不能只是说“忽略第一个事件”,因为如果团队玩家关闭或仅连接一个鼠标,那就是唯一的一个! )

关于为什么会发生这种情况以及我能做些什么来获得正常的鼠标事件有什么想法吗?

I'm using teamplayer, which lets you connect more mice to your computer to be used simultaneously. I'm also using pyHook to capture mouse events, with the following code:

import pyHook
import pythoncom

def onclick(event):
  # called when mouse events are received
  print 'MessageName:',event.MessageName
  print 'Message:',event.Message
  print 'Time:',event.Time
  print 'WindowName:',event.WindowName
  print 'Position:',event.Position
  print '---'
  return True

hm = pyHook.HookManager()
hm.MouseLeftDown = onclick
hm.MouseLeftUp = onclick
hm.HookMouse()
pythoncom.PumpMessages()

The code works fine without teamplayer - it detects the mouse button down and up accurately. If I start teamplayer while the program is running, then it continues to work well, this time detecting clicks from both mice accurately.

However, if I start the program after teamplayer is started, then every mouseclick becomes double:

MessageName: mouse left down
Message: 513
Time: 7231317
WindowName: None
Position: (673, 367)
---
MessageName: mouse left down
Message: 513
Time: 7231317
WindowName: None
Position: (673, 367)
---
MessageName: mouse left up
Message: 514
Time: 7231379
WindowName: None
Position: (673, 367)
---
MessageName: mouse left up
Message: 514
Time: 7231379
WindowName: None
Position: (673, 367)

This would be OK - I could detect clicks with the same timestamp and ignore the second one. However, when I click with a different mouse, the pattern is strange:

MessageName: mouse left down
Message: 513
Time: 7305916
WindowName: C:\Python25\python.exe
Position: (569, 306)
---
MessageName: mouse left down
Message: 513
Time: 7305916
WindowName: C:\Python25\python.exe
Position: (722, 365)
---
MessageName: mouse left up
Message: 514
Time: 7309598
WindowName: C:\Python25\python.exe
Position: (722, 365)
---
MessageName: mouse left up
Message: 514
Time: 7309598
WindowName: C:\Python25\python.exe
Position: (722, 365)

That is, the first down event uses the coordinates from the last up event! The problem is also that the wrong event is first, making it harder to detect the correct one (I can't just say "ignore the first event", because if teamplayer is off or only one mouse is connected, that's the only one!)

Any ideas as to why this might be happening, and what I can do to get normal mouse events?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

空心空情空意 2024-09-18 16:51:24

多个想法:

  • 检测同一时间戳的重复点击,并忽略其中的第一个。这需要将处理延迟到一个时间点之后,这
  • 确实会使代码中的问题稍微复杂化...您可以更改 pyhook 源来处理重复项;在其源代码中进行调试可能会让您更深入地了解正在发生的情况。简单地看一下,您可以在其 HookManagerMouseSwitch 函数中通过排队和刷新消息来完成此操作。一旦你弄清楚那里发生了什么,你就可以包装该对象,这样你就不必修改 pyhook
  • 它很可能是 SetWindowsHookEx API 就是这样生成重复事件;因为团队玩家正在用多只老鼠做一些复杂的事情。将此情况报告给团队成员;他们可能在某些时候有兴趣从他们这边修复它

Multiple ideas:

  • Detect duplicate clicks from the same timestamp, and ignore the first of them. This would require delaying the processing until one time tick later which does complicate matters slightly in your code...
  • You could alter the pyhook source to handle the duplicates instead; debugging inside their source code may give you more insight as to what's happening. From looking at it briefly you could do this in their HookManager's MouseSwitch function by queuing and flushing messages. Once you've worked out what's happening there, you could wrap that object so you don't have to have a modified pyhook
  • It's most likely that the SetWindowsHookEx API is what is generating the duplicate events; because teamplayer is doing something complicated with multiple mice. Report this to teamplayer; they may at some point be interested in fixing it from their side
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文