[Techtalk] Detection of mouse-clicks and/or keypresses in Python

Akkana Peck akkana at shallowsky.com
Thu Jun 4 01:28:53 UTC 2015

I sent this to techtalk this morning but it seems to have vanished,
so I'm trying again (and CCing David this time in case it doesn't go
through).  Probably both copies will show up tomorrow or the next day.
I wish I knew why our lists sometimes did that. Anyway:

David Sumbler writes:
> I want to write a simple program in Python 3, for my own use, which will
> detect mouse-clicks (or possibly key-presses from the keyboard) and
> record the exact time of each event.
> In principle this should be easy enough, except that I do not know how
> to detect the events themselves.

I'm assuming you want to do this without a GUI window, since it's
easy in pygtk or tkinter or other toolkits.

I did that once for a Raspberry Pi program I wrote, where I wanted
to use a mouse (wheel and buttons) as a control for a headless
program (no monitor at all). I wrote a little MouseReader class
that reads events from the mouse device:

It's Python 2, but it might at least help get you started.


More information about the Techtalk mailing list