python mouse event recorder

Discussion in 'Web Programming' started by POLO, Dec 26, 2007.

python mouse event recorder
  1. Unread #1 - Dec 26, 2007 at 5:07 AM
  2. POLO
    Referrals:
    2

    POLO Guest

    python mouse event recorder

    hello,

    i'm trying to do a mouse recorder.
    I am using Pyhook to do that and i have a problem with the hookmanager.
    When I stop the keyboard and mouse hook, my GUI don't take the focus and crash.
    Code:
    from Tkinter import *
    import pyHook, pythoncom
    import threading
    
    class Application():
        def __init__(self):
            self.fen = Tk()
            self.fen.title("hook")
    ##        self.listederoul = Listbox(self.fen, )
            self.texte = Text(self.fen, width = 50, height = 20, bg ='white')
            self.texte.pack(side = TOP, padx = 5, pady = 5)
            self.bouton1 = Button(self.fen, text="Enregistrement", command = self.Enregistrement).pack(side = LEFT)
            self.bouton2 = Button(self.fen, text ="Quitter", command = self.fen.destroy).pack(side=LEFT)
            self.fen.mainloop()
            
        def Eventsouris(self, event):
            self.dico = []
            self.dico.append(event.Position)
            print self.dico
            return True
            
        def Enregistrement(self):
            self.hm = pyHook.HookManager()
            self.hm1 = pyHook.HookManager()
            self.hm.MouseLeftDown = self.Eventsouris
            self.hm1.KeyAll = self.Stophook
            self.hm.HookMouse()
            self.hm1.HookKeyboard()
            pythoncom.PumpMessages()
    
        def Stophook(self, event):
            print "fonction stophook appell?e"
            self.hm.UnhookMouse()
            self.hm1.UnhookKeyboard()
            self.texte.focus_set()
    if someone have ideas thanks...
     
  3. Unread #2 - Dec 26, 2007 at 1:07 PM
  4. cp
    Joined:
    Jan 30, 2007
    Posts:
    3,278
    Referrals:
    6
    Sythe Gold:
    0

    cp an cat
    Banned

    python mouse event recorder

    Glad someone other than me is doing python :)

    It won't work. You can't have two infinite loops running at the same time (PumpMessages() and mainloop()).

    You can use threads, however, but the result may be buggy. I see that you're using threads as well, I just don't see why it isn't working.

    I don't get what you're currently trying to do; if you could tell me, I'll be able to help more.
     
  5. Unread #3 - Dec 27, 2007 at 3:11 AM
  6. POLO
    Referrals:
    2

    POLO Guest

    python mouse event recorder

    hello cp_

    i'am trying to retrieve the mouse's position when clic.
    Mycode here works well except when I stop the hook with "Stophook" function. I don't know why because I stop the to hook the mouse and the keyboard.
    Code:
    from Tkinter import *
    import pyHook, pythoncom
    import threading
    
    class Application():
        def __init__(self):
            self.fen = Tk()
            self.fen.title("hook")
    ##        self.listederoul = Listbox(self.fen, )
            self.texte = Text(self.fen, width = 50, height = 20, bg ='white')
            self.texte.pack(side = TOP, padx = 5, pady = 5)
            self.bouton1 = Button(self.fen, text="Enregistrement", command = self.Enregistrement).pack(side = LEFT)
            self.bouton2 = Button(self.fen, text ="Quitter", command = self.fen.destroy).pack(side=LEFT)
            self.h = 0
            self.fen.mainloop()
            
        def Eventsouris(self, event):
            self.x = event.Position[0]
            self.y = event.Position[1]
            self.dico[self.x] = self.y
            print self.dico
            for g in self.dico.items():
                self.h = self.h + 1
            print self.h
            return True
            
        def Enregistrement(self):
            self.hook = pyHook.HookManager()
            self.hook1 = pyHook.HookManager()
            self.hook.MouseLeftDown = self.Eventsouris
            self.hook1.KeyAll = self.Stophook
            self.hook.HookMouse()
            self.hook1.HookKeyboard()
            self.dico = {}
            pythoncom.PumpMessages()
    
        def Stophook(self, event):
            print "fonction stophook appell?e"
            self.hook.UnhookMouse()
            self.hook1.UnhookKeyboard()
            monfichier = open('C:/hook/hooklist.txt', 'w')
            for i in self.dico:
                monfichier.write(str(i)+':')
            monfichier.close()
            sys.exit()
            
            
    ##        self.message = tkMessageBox.showwarning("FIN ENREGISTREMENT", "L'engistrement est termin?")
    
    if __name__== '__main__':
        appl = Application()
                
    I try to register the mouse position when clic. I tried to do this with threads but it doesn't.
    If you have ideas...
    PS: Sorry, my english is very bad.
     
  7. Unread #4 - Dec 27, 2007 at 3:49 AM
  8. cp
    Joined:
    Jan 30, 2007
    Posts:
    3,278
    Referrals:
    6
    Sythe Gold:
    0

    cp an cat
    Banned

    python mouse event recorder

    Your english is just fine.

    Currently, I am on a linux computer, so it could be a few days before I can actually test out your code :/
     
  9. Unread #5 - Dec 27, 2007 at 7:14 AM
  10. POLO
    Referrals:
    2

    POLO Guest

    python mouse event recorder

    thanks for your quick response. If you can test it in few days its ok for me.
    I look the forum usually.
    thanks
     
  11. Unread #6 - Jan 7, 2008 at 4:08 AM
  12. POLO
    Referrals:
    2

    POLO Guest

    python mouse event recorder

    Hey CP_ ! Are you back?
    I'm looking for you response everyday :D
     
  13. Unread #7 - Jan 10, 2008 at 4:01 AM
  14. POLO
    Referrals:
    2

    POLO Guest

    python mouse event recorder

    Just an idea. If i create a 2 classes, one for the GUI and one for "Enregistrement". Is it ok, if i call the class "enregistrement" and after if I give the focus to the GUI, the class "enregistrement" will stop?
     
  15. Unread #8 - Jan 21, 2008 at 2:59 AM
  16. POLO
    Referrals:
    2

    POLO Guest

    python mouse event recorder

    hello,
    hey cp_, do you forget me?
     
  17. Unread #9 - Jan 21, 2008 at 2:15 PM
  18. cp
    Joined:
    Jan 30, 2007
    Posts:
    3,278
    Referrals:
    6
    Sythe Gold:
    0

    cp an cat
    Banned

    python mouse event recorder

    Sorry...

    For a script like this, you would need some high leveled threading, something which just isn't available in Python.

    C++ I believe can do this, however, Python, as far as I know, cannot.
     
  19. Unread #10 - Jan 22, 2008 at 9:07 AM
  20. POLO
    Referrals:
    2

    POLO Guest

    python mouse event recorder

    ok thanks cp_. If I find a solution, I post a reply.
    :(
     
< Looking for webdesigner | Directory. >

Users viewing this thread
1 guest


 
 
Adblock breaks this site