I am working with a keyloger procedure to capture user inputs even mouse has no focus on my app windows.

I make it work using that code, the problem is that

event = handle.read(6)

only reports clicks when buffer is full.
Its not working ok because there is a lot of missing clicks.

Whats the way to turn this into a real event handler procedure?

Thank you.

#!/usr/bin/env python

from threading import Thread

CLICK_LEFT  = ('%c%c%c%c%c%c')%(0x9,0x0,0x0,0x8,0x0,0x0)
CLICK_RIGHT = ('%c%c%c%c%c%c')%(0xA,0x0,0x0,0x8,0x0,0x0)
CLICK_MED   = ('%c%c%c%c%c%c')%(0xC,0x0,0x0,0x8,0x0,0x0)

    handle = open("/dev/input/mouse1", 'rb')
except Exception, e:
    print "Error 0:", e

ACTIONS = {'L':0,'M':1,'R':2}

def mouseClicks():

        def th():
            while True:
                    event = handle.read(6)
                except Exception, e:
                    print "Error 1: ", e

                if CLICK_LEFT == event:
                    print "L click", ACTIONS['L']
                elif CLICK_RIGHT == event:
                    print "R click", ACTIONS['R']
                elif CLICK_MED == event:
                    print "M click", ACTIONS['M']

            t = Thread(target=th,args=())
        except Exception, e:
            print e

def main():

if __name__ == "__main__":


My development is in Linux, Debian more exactly. Recently I was using simple python keylogger portions of code, but, in actual Xorg version the Record extension was disabled. I need to move over a xorg independant solutrion.