Issue with Hotkey

Asked by bitboy on 2021-02-20

I'm trying to set a hotkey to end a script which is running in the IDE.

===========================
def exitHotkey(event):
    print "Exit"
    exit()

Env.addHotkey(Key.ESC, 0, exitHotkey)
print "start"
wait(60)
print "stop"
===========================

But if i press the Hotkey, the script and the IDE is closed and i have to restart it.
I tried other hotkeys but no success. Calling the function manually works fine.

OS: Linuxmint 20.1 Cinnamon
Sikulix: 2.0.4

Question information

Language:
English Edit question
Status:
Answered
For:
SikuliX Edit question
Assignee:
No assignee Edit question
Last query:
Last reply:
Revision history for this message
RaiMan (raimund-hocke) said :
#1

exit() cannot be used in the handler, since it stops the application here (hence the IDE).

You have to tell the main script, that the hotkey was pressed simply with a global variable:

def exitHotkey(event):
    global shouldStop
    Do.popup("stopping", 1)
    shouldStop = True

Env.addHotkey(Key.ESC, 0, exitHotkey)

shouldStop = False
while not shouldStop:
    wait(1)

Revision history for this message
bitboy (zeus557) said :
#2

Thx for your answer. What i had in mind was my own selected Hotkey for the combination SHIFT + ALT + c which immediately stops the running script and shows the IDE again.

Imagine having some time consuming functions in your main loop, so it could take a while until the exit() is really executed. This could be confusing.

Can you help with this problem?

Provide an answer of your own, or ask bitboy for more information if necessary.

To post a message you must log in.