简体   繁体   中英

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, eg 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.

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()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM