eco_bach
Posts: 5
Joined: Fri Nov 22, 2013 3:07 pm

Detect mouse coordinates OUTSIDE of any GUI using Python

Thu Jul 03, 2014 3:03 pm

Hi
Am using the Python Tkinter module, with Raspbian installed on my Rpi. I can successfully grab the mouse xy but only within a windowed GUI environment. Does anyone know if its possible to use Python to grab mouse xy, and mouse down xy coordinates OUTSIDE of any GUI? What about the pygame module, does it have to run within a GUI environment to detect mouse position?

asandford
Posts: 1998
Joined: Mon Dec 31, 2012 12:54 pm
Location: Waterlooville

Re: Detect mouse coordinates OUTSIDE of any GUI using Python

Thu Jul 03, 2014 4:15 pm

eco_bach wrote:Hi
Am using the Python Tkinter module, with Raspbian installed on my Rpi. I can successfully grab the mouse xy but only within a windowed GUI environment. Does anyone know if its possible to use Python to grab mouse xy, and mouse down xy coordinates OUTSIDE of any GUI? What about the pygame module, does it have to run within a GUI environment to detect mouse position?
No, you can't (unless you write your own driver for the mouse), you need some sort of WIMP service to provide those coordinates to you.

You can test this (no WIMP system running) by booting your pi to the CLI with a mouse and keyboard plugged it. The KB works, the mouse doesn't. Type startx and your mouse appears.

Back in the day, STOS had mouse support outside GEM, but it used its own drivers.

User avatar
PeterO
Posts: 5736
Joined: Sun Jul 22, 2012 4:14 pm

Re: Detect mouse coordinates OUTSIDE of any GUI using Python

Thu Jul 03, 2014 6:03 pm

eco_bach wrote:Hi
Am using the Python Tkinter module, with Raspbian installed on my Rpi. I can successfully grab the mouse xy but only within a windowed GUI environment. Does anyone know if its possible to use Python to grab mouse xy, and mouse down xy coordinates OUTSIDE of any GUI? What about the pygame module, does it have to run within a GUI environment to detect mouse position?
You can pick up raw mouse (and keyboard) events by reading from the appropriate device file in "/dev/input/by-id". For example on this machine I have a Logitech tracker ball and it shows up as
/dev/input/by-id/usb-Logitech_USB_Trackball-event-mouse

You can read blocks of data from that file that correspond to this C structure:
struct input_event {
struct timeval time;
__u16 type;
__u16 code;
__s32 value;
};

I can post some C code that I use to poll the mouse and generate my own button press and motion events for use in openGL ES applications where there is no GUI running to handle these events.

I'm not good enough at Python to be able to tell you how to do that in Python but it must be possible.....

EDIT: I'm looking at the "struct" library for Python as it looks like it can do the required transformation from the C structure into python variable.... I'll post code if I get it to work.

PeterO
Discoverer of the PI2 XENON DEATH FLASH!
Interests: C,Python,PIC,Electronics,Ham Radio (G0DZB),1960s British Computers.
"The primary requirement (as we've always seen in your examples) is that the code is readable. " Dougie Lawson

User avatar
PeterO
Posts: 5736
Joined: Sun Jul 22, 2012 4:14 pm

Re: Detect mouse coordinates OUTSIDE of any GUI using Python

Thu Jul 03, 2014 7:14 pm

Quick hack to read events from the mouse and write messages to the screen.

Documentation on the struct module is at
https://docs.python.org/2.7/library/str ... ule-struct

Code: Select all

#!/usr/bin/python

import struct
import binhex

# You'll need to find the name of your particular mouse to put in here...
file = open("/dev/input/by-id/usb-Logitech_USB_Trackball-event-mouse","rb")


while True:


    byte = file.read(16)
#    h = ":".join("{:02x}".format(ord(c)) for c in byte)
#    print "byte=",h

    (type,code,value) =  struct.unpack_from('hhi', byte, offset=8)

    if type == 1 and value == 1:
        if code == 272:
            print "LEFT PRESS"
        if code == 273:
            print "RIGHT PRESS"

    if type == 2:
        if code == 0:
            print "MOVE L/R",value
        if code == 1:
            print "MOVE U/D",value

HTH
PeterO
Discoverer of the PI2 XENON DEATH FLASH!
Interests: C,Python,PIC,Electronics,Ham Radio (G0DZB),1960s British Computers.
"The primary requirement (as we've always seen in your examples) is that the code is readable. " Dougie Lawson

Return to “General discussion”