JCastejon
JCastejon

Reputation: 53

[Python][evdev] Get cursor position after mouse click

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

Upvotes: 2

Views: 2807

Answers (1)

Neil
Neil

Reputation: 4049

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.

Upvotes: 2

Related Questions