Setup a hotkey for Sikuli to execute code

Asked by Maurice Richard

How would you setup hotkeys for Sikuli to execute your code.

For Example:

Control - Shift - N: Would run a function to do something
Control - Shift - E: Would run a function to do something else

So with Sikuli running, and the user presses Control - Shift - N ... would run a function.
Alternative, if they pressed Control - Shift - E, it would run a different function.
And if the user pressed nothing, Sikuli would just wait for that input.

Thanks!

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
Best RaiMan (raimund-hocke) said :
#1

a generic example.

To put the payload into the main loop makes things more transparent IMHO.

# hotkey to stop the script
hotKeyX = False; # global to communicate with main loop
def xHandler(event):
  global hotKeyX
  hotKeyX = True # tell main loop that hotkey was pressed
# add the hotkey with its handler
Env.addHotkey("x", KeyModifier.CTRL + KeyModifier.SHIFT, xHandler)

# function hotkey: something to do when pressed
hotKeyN = False;
def nHandler(event):
  global hotKeyN
  hotKeyN = True
Env.addHotkey("n", KeyModifier.CTRL + KeyModifier.SHIFT, nHandler)

# the main loop, that simply waits for pressed hotkeys
# which are then processed
count = 0;
while True:
  if (hotKeyX):
    popup("processing ctrl+shift+x: stopping")
    exit()
  if (hotKeyN):
    hotKeyN = False
    count += 1
    popup("processing ctrl+shift+n: %d" % count)
  wait(1)

Revision history for this message
RaiMan (raimund-hocke) said :
#2
Revision history for this message
Maurice Richard (flashfires) said :
#3

The hotkey script is perfect, very helpful!

Thanks RaiMan. :)

Revision history for this message
Maurice Richard (flashfires) said :
#4

Thanks RaiMan, that solved my question.