Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #19261 > unrolled thread

Re: PyHook to catch mouse events

Started byDave Angel <d@davea.name>
First post2012-01-23 08:24 -0500
Last post2012-01-23 08:24 -0500
Articles 1 — 1 participant

Back to article view | Back to comp.lang.python

This discussion starts older than the indexed window; earlier articles aren't shown. The article labeled Started by below is the oldest one visible, not the original post.


Contents

  Re: PyHook to catch mouse events Dave Angel <d@davea.name> - 2012-01-23 08:24 -0500

#19261 — Re: PyHook to catch mouse events

FromDave Angel <d@davea.name>
Date2012-01-23 08:24 -0500
SubjectRe: PyHook to catch mouse events
Message-ID<mailman.4963.1327325089.27778.python-list@python.org>
On 01/23/2012 07:06 AM, Neru Yume wrote:
> Using PyHook to record mouse events, one has to add quite a few lines to set up a hook, and as far as I have experienced, if one uses time.sleep() or some other function that spends some time doing something, the program freezes my computer completely while doing this (the cursor starts moving slowly, and so on).
>
>
> Also: Ctrl+c does not work, I have to click the cross to exit the program, which is horrible when combined with the freezing mentioned earlier.
>
>
> Is there a way to avoid the program freezing up? Using the win32api somehow, rather than PyHook? (I only know how to generate input with win32api, not catch output).
>
>
> import pythoncom, pyHook
> class record(object):
>      def OnMouseEvent(self, 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 '---'
> #time.sleep(1) #If I uncomment this, running the program will freeze stuff, as mentioned earlier.
>          return True
>
> recordit = record()
> hm = pyHook.HookManager()
> hm.MouseAll = recordit.OnMouseEvent
> hm.HookMouse()
> pythoncom.PumpMessages()
>
This is the nature of event-driven systems.  When you hook into the 
Windows system, you're expected to carve your event handlers into 
something quick.  For normal Python gui programming, breaking the rule 
will only affect your own program.  Pyhook just does the same thing on a 
system-wide scale.


Are you sure you need global events at all?

If you want to do useful work in response to particular global mouse 
events, you'll have to arrange to get control some other way.  Normally 
this is done by posting pseudo-events in your process's message queue, 
so you'll get control again, and can continue working on the problem.  I 
don't know the specific details for pyhook, since I don't run Windows 
any more, and pyhook is not part of Python itself.


-- 

DaveA

[toc] | [standalone]


Back to top | Article view | comp.lang.python


csiph-web