Skip to content Skip to sidebar Skip to footer

How Can I Make A While True Break If Certain Key Is Pressed? [python]

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 onKeyb

Solution 1:

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):
    ifevent.KeyID == 115:      #F4
        startTimer(doTimer, 2)
    ifevent.KeyID == 113:
        stopTimer()

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

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

Solution 2:

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)

Post a Comment for "How Can I Make A While True Break If Certain Key Is Pressed? [python]"