2

I have a problem for which I need to capture all events coming from my mouse. After browsing the web, I read about evdev module and gave it a try.

I have now a script where I grab all events from my mouse, to prevent other interactions with other windows (important point in my initial problem). With it, I can read events when a button is clicked, and when my mouse moves. But I do not find how to get the cursor position when the button is clicked.

#!/usr/bin/env python
# -*- coding: utf8 -*-

from evdev import InputDevice, categorize, ecodes
from os import listdir
from os.path import isfile, isdir, exists, join

def my_list():
    print('*** my_list(): begin.')
    devices = map(InputDevice, list_devices())
    for dev in devices:
        print('%-20s %-32s %s' % (dev.fn, dev.name, dev.phys))
        print('*** my_list(): end.')

def monitor_device(dev):
    for event in dev.read_loop():
        if event.type == ecodes.EV_KEY:
            print(categorize(event))
            print(event)

if __name__ == "__main__" :
    dev_path = '/dev/input/event17'
    if(exists(dev_path)):
        device = InputDevice(dev_path)
        try:
            device.grab()
            monitor_device(device)
        except KeyboardInterrupt :
            device.ungrab()
            print('User aborted the program.')

How can I do it with evdev? If I can't, is there another way to do so?

Any help would be greatly appreciated. :)

Claudio
  • 10,614
  • 4
  • 31
  • 71
JCastejon
  • 53
  • 4
  • You are reading a raw event stream from the hardware. There are no coordinates in it anywhere. If you need to know where your window system pointer is, you need to talk to your window system, not to the hardware. You are not asking your SATA controller for files and folders, are you? – n. m. could be an AI Aug 01 '13 at 16:57
  • I get what you are saying. So, is there a way for my program to be the only to get the mouse clicks, and ask the window system the position of the cursor at this moment? – JCastejon Aug 03 '13 at 18:37
  • You can do that, but for uniformity you may want to ask X11 about both clicks and coordinates. – n. m. could be an AI Aug 04 '13 at 14:08

1 Answers1

1

You can listen for ABS_MT_POSITION_X and ABS_MT_POSITION_Y events, and store the values in x, y variables. Then when you get a BTN_TOUCH event you know what the location was.

Neil
  • 3,899
  • 1
  • 29
  • 25