1

At the moment I'm trying to use Python to detect when the left mouse button is being held and then start to rapidly send this event instead of only once. What I basically want to do is that when the left mouse button is held it clicks and clicks again until you let it go. But I'm a bit puzzled with the whole Xlib, I think it's very confusing actually. Any help on how to do this would be really awesome. That's what I've got so far:

#!/usr/bin/env python

import Xlib
import Xlib.display

def main():
    display = Xlib.display.Display()
    root = display.screen().root
    while True:
        event = root.display.next_event()
        print event

if __name__ == "__main__":
    main()

But there is unfortunately no output in the console. After a quick search on the internet I found the following:

root.change_attributes(event_mask=Xlib.X.KeyPressMask)
root.grab_key(keycode, Xlib.X.AnyModifier, 1, Xlib.X.GrabModeAsync,
              Xlib.X.GrabModeAsync)

This is seemingly import to catch a special event with the given keycode. But firstly what keycode does the left-mouse click have, if any at all? And secondly how can I detect when it is being held down and then start sending the mouseclick event rapidly. I would be really grateful for help. (Maybe a way to stop this script with a hotkey would be cool aswell...)

cryzed
  • 151
  • 1
  • 4
  • 7
  • Here's an [official example](https://sourceforge.net/p/python-xlib/code/HEAD/tree/trunk/examples/record_demo.py) demonstrating the recording of clicks, mouse movement and keypresses. – opyate Apr 01 '16 at 11:47

1 Answers1

5

Actually you want Xlib.X.ButtonPressMask | Xlib.X.ButtonReleaseMask, to get events for button presses and releases (different from key presses and releases). The events are ButtonPress and ButtonRelease, and the detail instance variable gives you the button number. From when you get the press event, to when you get the release event, you know the button is being held down. Of course you can also receive key events and do something else (e.g. exit your script) when a certain key is pressed.

Edit: this version works fine for me, for example...:

import Xlib
import Xlib.display

def main():
    display = Xlib.display.Display(':0')
    root = display.screen().root
    root.change_attributes(event_mask=
        Xlib.X.ButtonPressMask | Xlib.X.ButtonReleaseMask)

    while True:
        event = root.display.next_event()
        print event

if __name__ == "__main__":
    main()
Alex Martelli
  • 854,459
  • 170
  • 1,222
  • 1,395
  • So how would I go about specifying "root.grab_key"? – cryzed Jul 18 '09 at 21:54
  • Just tried several combinations, nothing worked :(. Could you do me a favor and quickly write the script or atleast the base? Elsewise I know it would take for forever until I finally get it running although it isn't all that complicated. I would be really grateful. – cryzed Jul 18 '09 at 22:36
  • See my answer's edit. As you don't want to grab a key, there's of course no need to call grab_key. BTW, for more on grab-key see http://tronche.com/gui/x/xlib/input/XGrabKey.html (more generally, you need X11 familiarity & docs to use Python Xlib successfully). – Alex Martelli Jul 18 '09 at 23:42
  • 3
    I use this code, and got: : code = 10, resource_id = 256, sequence_number = 9, major_opcode = 2, minor_opcode = 0 what is wrong? – linjunhalida Jul 09 '10 at 01:47
  • @linjun, unfortunately X11's docs aren't the best, so (besides the fact that there's an attempt to access a private resource) it's hard to know what's going on -- esp. when you don't provide the full traceback, without which debugging any Python issue is a nigthmare! I know it won't fit in a comment -- you need to open a new question to copy and paste the traceback if you want any chance to get help. (As long as you're at it, giving us complete, precise details on your platform, Python version, etc, is, _as usual_, an excellent idea too). – Alex Martelli Jul 09 '10 at 02:20
  • it does not seem to work any more (ubuntu 13.10 & Python 2.7.5). The snippet just prints . I believe that it needs some sort of event handler – moin moin Apr 05 '14 at 07:33