Setup a hotkey for Sikuli to execute code

Asked by Maurice Richard on 2017-02-15

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:
Sikuli Edit question
Assignee:
No assignee Edit question
Solved by:
RaiMan
Solved:
2017-02-16
Last query:
2017-02-16
Last reply:
2017-02-16
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)

Maurice Richard (flashfires) said : #3

The hotkey script is perfect, very helpful!

Thanks RaiMan. :)

Maurice Richard (flashfires) said : #4

Thanks RaiMan, that solved my question.