Using multiple hotkeys without popups? Is this possible?

Asked by Sam

I would like to call any of 3 hotkey functions in my script at any time. Currently I need to go in order.

I need to use the first hotkey, press okay on the popup.
Then I can use the second hotkey, press okay.
Then I can use the third hotkey.

I was hoping I could use any of the three hotkeys at any time without restarting the script. Do you have any suggestions for this?

Thank you

Question information

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

how does your current script look like?

What do you mean by hot-keys?

Revision history for this message
Sam (staffo08) said :
#2

Eh, I guess I can run them all at the same time. I messed up the first few times I suppose, but now it seems to be working. I don't really mind having a few popups in the background I suppose.

Revision history for this message
Sam (staffo08) said :
#3

scr2 = Screen(2)

#scr2.click("1443724310615.png")

data= [None]*33 # creating a list full of 0s

def CopyPage(event):
    scr2.click("1444047041130.png")

   # data= [None]*34 # creating a list full of 0s
   # data[12]= "Unknown" # should reset clipboard like stated below - temp fix
    x = 0
    for x in range(0, len(data),1):
   # App.setClipboard(data[x]) # sends data to keyboard
     # wasn't using e== before that's why swap wasn't working but != was

        type(Key.TAB)
        type("c", KeyModifier.CTRL)
        data[x] = Env.getClipboard()
        App.setClipboard("0")
        # should clear clipboard or set to known value here - this would avoid repeats in front of no copies like heating system
        ++x
        print data[x]
        print x
    scr2.click("1444055393604.png")

    type(Key.TAB)
    type(Key.TAB)
    type(Key.TAB)
    x=0
    print data[0]
    for x in range(0, len(data),1):
   # App.setClipboard(data[x]) # sends data to keyboard
     # wasn't using e== before that's why swap wasn't working but != was
        if data[x] == "0" :
            type(Key.TAB)
            ++x

        else:

            paste(data[x])

            type(Key.TAB)
            ++x
            print data[x]

        #Passive Hotkeys
Env.addHotkey(Key.NUM1,KeyModifier.CTRL, CopyPage)
popup("Press Ctrl+num1 instead of clicking a button") # need this popup to run in the background and the script can continue to run in the background

def PasteCalendar(event):

    paste(data[17])
    type(",")
    paste(data[11])
    type(",")
    paste(data[23])
    type(",")
    paste( data[24])

Env.addHotkey(Key.NUM2,KeyModifier.CTRL, PasteCalendar)
popup("Press Ctrl+num2 instead of clicking a button")

Thanks for the reply RaiMan. I can press all my hotkeys at once now. I thought there was something wrong with the popups and I'd rather not have them, but I suppose I do need some way to exit the script.

Everything is working now, but I've posted my (very sloppy) code above. Thanks again for the reply

Revision history for this message
Sam (staffo08) said :
#4

Actually, I stand corrected. I can only run the second script after I have pressed Okay to the first script. Is there anyway to be able to run both of them at any time?

Revision history for this message
Best RaiMan (raimund-hocke) said :
#5

ok, I think, I understand your problem.

If you want to run a script, that only implements hotkeys and does nothing else, then this is a possible generic structure:

shouldRun = True

def handler1(event):
    # do something if the corresponding hotkey is pressed

def handler2(event):
    # do something if the corresponding hotkey is pressed

... more of them if needed

def handlerStop():
    global shouldRun
    shouldRun = False

Env.addHotkey(Key.NUM1,KeyModifier.CTRL, handler1)
Env.addHotkey(Key.NUM2,KeyModifier.CTRL, handler2)

... more of them if needed

Env.addHotkey("x",KeyModifier.CTRL + KeyModifier.ALT, handlerStop) # press ctrl-alt-x to stop the script

# if you want:
popup("hotkeys are active now - click OK")

while shouldRun: # keep the script alive until the stop key is pressed
    wait(1)

# if you want:
popup("hotkeys will be deactivated after clicking ok")

activated hotkeys are auto-inactivated when the script ends.

Revision history for this message
Sam (staffo08) said :
#6

Thanks RaiMan, that solved my question.

Revision history for this message
Sam (staffo08) said :
#7

Thanks so much RaiMan. Next time I have time to work on the script I'll give this a try and report back

Revision history for this message
Sam (staffo08) said :
#8

This works exactly how I'd like now. Thanks so much!