Simple question from Python Newbie

Bengt Richter bokr at oz.net
Mon Dec 15 12:55:27 EST 2003


On Sun, 14 Dec 2003 20:34:34 -0800, Tim Roberts <timr at probo.com> wrote:

>"Phil D." <phildog22 at yahoo.com> wrote:
>>
>>Hello Python Hackers!
>> 
>>I just started working with Python a few days ago and have a simple 
>>question that I can't seem to figure out.  I am trying to build an
>>application to monitor my mouse moving habits while I'm surfing
>>the web for a project at MIT and I can't seem to figure out how
>>to capture mouse clicks.
A lot will depend on what you mean by "mouse moving habits" ;-)
Do you really mean you want to track your surfing? Or are you interested
in such things as copy/paste or drag/drop from the browser to other apps?
Be prepared to search for unique title bar text and such kludging to identify
windows, etc. ;-/
>
>Actually, this is not a particularly simple question.  The issue is that it
>isn't related to Python at all: this is an operating system issue.  An
>application is expected to be interested only the events that relate
>directly to that application.  Doing so on a system wide basis is usually
>only interesting for toy apps, such as the one you want to build.
>
>Fortunately, Windows provides a way to do that: it's called a "hook".  In
>this case, what you want is the "journal record hook" (WH_JOURNALRECORD),
>which gets all key and mouse events in the system.  This requires a
>callback, which means it needs C help.  I don't know if it is implemented
>in any of the Win32 extensions; I will have to investigate that (and hope
>someone like Mark Hammond posts the canned answer).

I don't have a canned answer, but I think the OP will want to look at
the docs for

HHOOK SetWindowsHookEx(
  int idHook,        // type of hook to install
  HOOKPROC lpfn,     // address of hook procedure
  HINSTANCE hMod,    // handle to application instance
  DWORD dwThreadId   // identity of thread to install hook for
);
 
(for which WH_JOURNALRECORD is one of many possible idHook values to pass).
Seems like WH_CBT and/or WH_MOUSE might be useful to consider also. I imagine
there will be some "interesting" problems when you start monitoring events from multiple
threads and processes.

If I had to write it, I think I would make sure I understood the spy sample app
that comes with MSVC++6.0, of which it is said
"""
Spy demonstrates the following techniques: 

Using a system message hook.

Using the WM_COPYDATA message to pass data to another application.

Reading and writing the registry.

Creating a thread.

Creating a DLL for the hook. 
"""
Then decide how I really want to access the data. And/or control the whole thing through
a python module, so you could import pyspy in a separately running python process and
monitor all the mouse activity in some sensible way. Maybe so the monitoring thread could
hang on a queue until there was synchronized buffered data available to grab.

If you (OP) describe what you'd like a hypothetical pyspy module to do (and its interfaces), that
might be a good way to communicate your project goals.

One thing to decide is how to specify what you are interested in and what you want to ignore.
Run spyxx.exe (tool that comes with MSVC6.0) for ideas.

>
>>Thoughts anyone?  What's the easiest way to get mouse clicks outside of a GUI?
Raw mouse clicks won't mean all that much in terms of monitoring "browsing habits," unless you
can know what windows and menus and widgets are being accessed, and what a click means.
>
>Your terminology is a bit confusing.  The entire desktop is "a GUI".  What
>you really want is to get mouse clicks outside of your own process.
And probably not just mouse clicks, so what hooks to use will depend on what
information is really sought.

Regards,
Bengt Richter




More information about the Python-list mailing list