Okay, well I've been looking for at least an hour now for some straight-forward python-xlib documentation, and I have been unable to find any. Luckily...I have quite the easy problem.

If somebody would be able to help me create a simple script that would say "Hello" every time the left mouse button is clicked, I have the rest of what I would like to do figured out

This is what I have so far, to no avail:

#!/usr/bin/python

import Xlib
import Xlib.display

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

if __name__ == "__main__":
    main()

Thank you so much in advance.

Okay, well I've been looking for at least an hour now for some straight-forward python-xlib documentation, and I have been unable to find any. Luckily...I have quite the easy problem.

If somebody would be able to help me create a simple script that would say "Hello" every time the left mouse button is clicked, I have the rest of what I would like to do figured out

This is what I have so far, to no avail:

#!/usr/bin/python

import Xlib
import Xlib.display

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

if __name__ == "__main__":
    main()

Thank you so much in advance.

This should help you out. It's written in C++, but the Xlib wrapper for python is very light so it shouldn't be hard to translate.

Have fun.

It should be something with ButtonReleaseMask. Try this code and Google for ButtonReleaseMask if it doesn't work.

import Xlib
import Xlib.display

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 "Hello button press"

It should be something with ButtonReleaseMask. Try this code and Google for ButtonReleaseMask if it doesn't work.

import Xlib
import Xlib.display

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 "Hello button press"

woooee, just out of curiosity, what would be the requirements be to make Xlib work on a Windows machine? I see that this project is still active.

@woooee: Thanks for the code. However, it gives me the following error:

X protocol error:
<class 'Xlib.error.BadAccess'>: code = 10, resource_id = 117, sequence_number = 7, major_opcode = 2, minor_opcode = 0

With a search from Google, I have come up with almost nothing. The closest thing I could find is that the "hotkey" I am using is already bound with something? I'm not quite sure. A 'ps -e | grep X' reveals nothing that should be already consuming hotkeys.

@vegaseat: You might be looking for something like this.

@scru: Correct me if I'm wrong, but it seems you intended to include a link or something but didn't.

Thanks again.

what would be the requirements be to make Xlib work on a Windows machine?

The system calls would be different on Windows so the code would differ too much to make it practical. There probably is something similar but something for MS Windows with a Python wrapper as well is another matter

If somebody would be able to help me create a simple script that would say "Hello" every time the left mouse button is clicked, I have the rest of what I would like to do figured out

There is also pyMouse http://code.google.com/p/pymouse/

There is this example for xlib
http://ltool.svn.sourceforge.net/viewvc/ltool/trunk/LtMacroAPI.py?view=markup

I have only used curses for the very few times I have wanted to do something like this http://www.ibm.com/developerworks/linux/library/l-python6.html or termios

""" capture and print keyboard input
"""
import termios, sys, os
TERMIOS = termios
def getkey():
        fd = sys.stdin.fileno()
        old = termios.tcgetattr(fd)
        new = termios.tcgetattr(fd)
        new[3] = new[3] & ~TERMIOS.ICANON & ~TERMIOS.ECHO
        new[6][TERMIOS.VMIN] = 1
        new[6][TERMIOS.VTIME] = 0
        termios.tcsetattr(fd, TERMIOS.TCSANOW, new)
        c = None
        try:
                c = os.read(fd, 1)
        finally:
                termios.tcsetattr(fd, TERMIOS.TCSAFLUSH, old)
        return c

if __name__ == '__main__':
        print "type something...'q' to quit"
        s = ''
        while 1:
                c = getkey()
                if c == 'q':
                        break
                print "captured key", c, ord(c)
                s = s + c

        print s

Thanks for all the information, except they all deal only with keys, not the mouse (PyMouse, from what I have seen, only creates mouse-clicks and the like, but it does not detect them).

I have however worked out a type of solution. I used xbindkeys for it, instead of the python-xlib library. I guess my problem is solved now!

Thank you for your suggestions everybody.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.