Hotkeys not working accordingly

Asked by sami baig on 2018-01-19

I am using below VB script of excel to launch sikulix1.1.2 script with Hotkeys but I don't know why it is opening new workbook instead of calling "openChrome(event)" in sikuliX.

Excel VB script:
Private Declare Sub Sleep Lib "Kernel32" (ByVal ms As Long)
Sub Button1_Click()
    SendKeys "^n"
    Sleep 5000
End Sub

SikuliX script:
def openChrome(event):
    click("1516306798475.png")
# When the user pressed Ctrl+n, click the top-left chrome icon.
Env.addHotkey("n", KeyModifier.CTRL, openChrome)

Question information

Language:
English Edit question
Status:
Solved
For:
Sikuli Edit question
Assignee:
No assignee Edit question
Solved by:
sami baig
Solved:
2018-01-19
Last query:
2018-01-19
Last reply:
2018-01-19
RaiMan (raimund-hocke) said : #1

I guess, this is because Excel has focus and ctrl+n is defined so in Excel.

Use a hotkey combination, that is not used by excel:
ctrl + alt + n

for example (hope it is not used by excel ;-)

sami baig (gabbarmic) said : #2

Still not working with SendKeys "^%n" in excel :-|

RaiMan (raimund-hocke) said : #3

supposing you now have

SikuliX script:
def openChrome(event):
    click("1516306798475.png")
# When the user pressed Ctrl+n, click the top-left chrome icon.
Env.addHotkey("n", KeyModifier.ALT + KeyModifier.CTRL, openChrome)

is this the whole script?

It needs at least a waiting loop, so it stays active and listen for hotkeys.

Does it work if you press the hotkey manually while it is active?

sami baig (gabbarmic) said : #4

yes, below is my whole script and I've also tried it with waiting loop seems like "script02" but still it's not working.

Script:
def openChrome(event):
    click("1516306798475.png")
# When the user pressed Ctrl+n, click the top-left chrome icon.
Env.addHotkey("n", KeyModifier.ALT + KeyModifier.CTRL, openChrome)

script02(with waiting loop):
def openChrome(event):
    wait(2)
    click("1516306798475.png")

Env.addHotkey("n", KeyModifier.ALT + KeyModifier.CTRL, openChrome)

Excel VB script:
Private Declare Sub Sleep Lib "Kernel32" (ByVal ms As Long)
Sub Button1_Click()
    SendKeys "^%n"
    Sleep 5000

sami baig (gabbarmic) said : #5

-Does it work if you press the hotkey manually while it is active?

It not working at all .

RaiMan (raimund-hocke) said : #6

Script:
def openChrome(event):
    click("1516306798475.png")
# When the user pressed Ctrl+n, click the top-left chrome icon.
Env.addHotkey("n", KeyModifier.ALT + KeyModifier.CTRL, openChrome)

# this is the waiting loop
while True:
    wait(1)

if you start the script, it will run forever to be able to listen and react.

In this setup the script has to be killed, if no longer needed.

sami baig (gabbarmic) said : #7

Thanks RaiMan, you solved my problem but please clear one more picture, when I ran below script directly from SikuliX IDE, it is still running and IDE not displaying yet, so tell me how should I stop it ?

Script:
def openChrome(event):
    click("1516306798475.png")

Env.addHotkey("n", KeyModifier.ALT + KeyModifier.CTRL, openChrome)

while True:
    wait(1)

RaiMan (raimund-hocke) said : #8

as mentioned:
In this setup the script has to be killed, if no longer needed.

On Windows with the taskmanager.

But usually one adds a termination hotkey like this:

def stopRun(event):
    global shouldRun
    shouldrun = False

def openChrome(event):
    click("1516306798475.png")

Env.addHotkey("n", KeyModifier.ALT + KeyModifier.CTRL, openChrome)
Env.addHotkey("x", KeyModifier.ALT + KeyModifier.CTRL, stopRun)

shouldRun = True
while shouldRun:
    wait(1)

Now pressing ctrl-alt-x will end the script.

BTW:
it makes sense, to run such scripts from a commandline, so you have the IDE in parallel available for other things.