Simple question from Python Newbie

P

Phil D.

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. I know that I can capture mouse clicks using Tkinter or another GUI toolkit, but that won't work outside the GUI (in a web browser) as far as I can tell. I get the cursor position pretty easily with win32api.GetCursorPos(), but I can't figure out how to get mouse clicks.

Thoughts anyone? What's the easiest way to get mouse clicks outside of a GUI?

Thanks in advance!
Phil
 
T

Tim Roberts

Phil D. said:
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.

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).
Thoughts anyone? What's the easiest way to get mouse clicks outside of a GUI?

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.
 
C

Carl Banks

Tim said:
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).


In Linux, you can open the mouse device file and read to your heart's
content. (Or you can configure GPM to open a mouse repeater device,
and read from that.) It'll probably need some ioctl calls, but
nothing Python can't do wihtout help from C.

Figuring out how to read from the device and process the mouse
protocol is left as an exercise.
 
B

Bengt Richter

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.
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,785
Messages
2,569,624
Members
45,318
Latest member
LuisWestma

Latest Threads

Top