pythonpython-2.7functiontimepyhook

Python: Using timer with PyHook


I am attempting to use PyHook to disable keyboard and mouse while running the script, and I am having difficulties setting a timer function so that the keyboard and mouse will only be disabled for a predefined amount of time, e.g. 30 seconds, and then return to normal.

def windoow(event):
    while True:
        return False
        time.sleep(30)
        break

hm = pyHook.HookManager()
hm.MouseAll = windoow
hm.KeyAll = windoow
hm.HookMouse()
hm.HookKeyboard()
pythoncom.PumpMessages()

I am new to programming in general, and I am therefore hoping for an easy solution that I can learn from and understand.

Thank you.


Solution

  • Hmm, This is pretty nefarious but this does work. Your time.sleep() was never executing because it was placed after a return statement

    import pythoncom, pyHook, time
    start = time.time()
    time.clock()
    elapsed = 0
    
    def windoow(event):
        global elapsed
        if elapsed < 30:
           elapsed = time.time() - start
           time.sleep(1)
           return False
    
        return True
    
    hm = pyHook.HookManager()
    hm.MouseAll = windoow
    hm.KeyAll = windoow
    hm.HookMouse()
    hm.HookKeyboard()
    pythoncom.PumpMessages()