tags:

views:

75

answers:

2

My script make a while True: begin with the F4 pressed, but I want it to stop when the F2 is pressed, how can I do it?

I'm trying this (using pyhook) but doesn't work...

def onKeyboardEvent(event):
    if event.KeyID == 115:      #F4
        while True:
            selectAndCopy(468,722)
            getClipboard()
            time.sleep(2)
            if event.KeyID == 113:
                break
    return True
+1  A: 

You're not changing event within your loop, so you wouldn't expect event.KeyID to suddenly become 113 when it was 115 previously.

What you might do is, on handling an F4 keypress, start a timer that does the selectAndCopy every two seconds. When you get another event with an F2 keystroke, kill the timer.

It could look something like this:

def onKeyboardEvent(event):
    if event.KeyID == 115:      #F4
        startTimer(doTimer, 2)
    if event.KeyID == 113:
        stopTimer()

def doTimer():
    selectAndCopy(468,722)
    getClipboard()

You would have to provide or find implementations of startTimer() and stopTimer().

Greg Hewgill
could you give me an example?
Shady
I don't know anything about pyhook, so I'll make some stuff up and see what I can do.
Greg Hewgill
with threading.Timer I can make a timer, but it doesn't loop =/
Shady
A: 

Make key event which

  1. change variable True with F4 and if the variable is still True do new timer event for example in Tkinter

    mylabel.after(2000, process) # process is the function to do your stuff

  2. change variable False with F2 and cancels the timer (after_cancel)

Tony Veijalainen