Query if a key or key-combo is pressed

Asked by GZ on 2017-05-03

Is there a way to query what keys are pressed at the moment?

I know there is Env.isLockOn(Key.NUM_LOCK), but is there something similar I could use so Sikuli can detect ALT+SHIFT+C in a running terminal script?

Something like this?
if Env.isDown(Key.ALT, Key.SHIFT+ "c"):
    break

Question information

Language:
English Edit question
Status:
Solved
For:
Sikuli Edit question
Assignee:
No assignee Edit question
Solved by:
GZ
Solved:
2017-05-08
Last query:
2017-05-08
Last reply:
2017-05-08
masuo (masuo-ohara) said : #1

addHotkey feature may be able to solve your question.

http://sikulix-2014.readthedocs.io/en/latest/interaction.html?highlight=Hotkey

GZ (george-zador) said : #2

Thanks Masuo! Very good suggestion , but I'm looking for something the queries the pressed keys at a given point in the program.

For example my loop is carrying out what it has to, but if I want to pause one of the loops at the very end, I can just keep my shortcut combo pressed and when the loop reaches the point where it checks for pressed keys, it will recognise my combo and pause.
This way I can have a different shortcut to pause the loop at a different important point if I have to, and if I don't want to interrupt the loops I don't press my shortcut combos.

masuo (masuo-ohara) said : #3

You can use like this.

loopctl = True

def handler(event):
    global loopctl
    loopctl = False

Env.addHotkey(Key.END, KeyModifier.CTRL, handler)

while True:
    if loopctl == True:
        print "wait"
        sleep(1)
    else:
        break

GZ (george-zador) said : #4

Thanks Masuo, this is what I was looking for!

So if I add a hotkey, is it removed after I stop the sikuli script that is running in the terminal?
I'm asking if I have to be worried about having to make sure that I remove it manually?

RaiMan (raimund-hocke) said : #5

--- So if I add a hotkey, is it removed after I stop the sikuli script that is running in the terminal?
If the script is terminated in a way, that the final cleanup can be done internally (meaning a normal script end), then the HotKey manager is reset and frees the defined keys.
Anyways the hotkeys are bound to the Java runtime running in the background. So only in cases, where the SikuliX session is terminated without terminating the Java runtime and not having run cleanup, there might be problems.
The latter should only be possible, if you do some "experimental" things or run into a crash, that is not handled correctly.

With "normal" scripting everything should be fine so.

GZ (george-zador) said : #6

Thanks RaiMan, so if I understand you correctly, Java will do the cleanup by itself after I stop my script, I don't have to write the Env.remove myself?

RaiMan (raimund-hocke) said : #7

yes

GZ (george-zador) said : #8

Great, thanks!

GZ (george-zador) said : #9

Although it would be great to have a feature in future releases that is to detect any keys that are down at the moment, something like getKesDown() or something!
Where can I log a feature request for that?

RaiMan (raimund-hocke) said : #10

No need to post a request:
In version 2 I will integrate https://github.com/kwhat/jnativehook for different features (e.g. a recorder), that allows, to give feedback, what is going on with mouse and keyboard at the system level.

RaiMan (raimund-hocke) said : #11

But if you ever want:
post a bug with a title like
[request] want a feature like getKeyPressed()

or post an issue on github (https://github.com/RaiMan)