开发者

teamplayer and pyhook interacting strangely

开发者 https://www.devze.com 2023-01-09 02:25 出处:网络
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:

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:开发者_StackOverflow 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?


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
0

精彩评论

暂无评论...
验证码 换一张
取 消