KeyModifiers causes a click()

Asked by Singah on 2019-07-17

I have an issue that I really can't figure out what happens. I have the following method which I believe is fairly straight forward, I'd like to click at a tree in my program and then use the keyboard to guide sikuli to the right items in the tree. It works when doing these combination manually, but when sikuli runs it, the first click seems to repeat itself each time I call for the type(KeyModifier.SHIFT, Key.DOWN) command. Code and log follows:

def select_item_in_cluster(startpoint, endpoint):
    click(Pattern('tree_image.png').exact())
    type('folder_in_tree')
    type(Key.RIGHT) #open a subfolder
    type(startpoint) #click at an item in the subfolder - 7 in this case
    for __ in range(int(startpoint), int(endpoint)): #mark a bunch of items
        type(KeyModifier.SHIFT, Key.DOWN)
        sleep(1)
    type(Key.ENTER)

From the sikuli logfile I get the following:
[log (7/17/19 2:16:10 PM)] CLICK on L(44,234)@S(0)[0,0 1920x1200]
[log (7/17/19 2:16:10 PM)] TYPE "folder_in_tree"
[log (7/17/19 2:16:12 PM)] TYPE "#RIGHT."
[log (7/17/19 2:16:13 PM)] TYPE "7"
[log (7/17/19 2:16:14 PM)] CLICK on L(44,234)@S(0)[0,0 1920x1200]
[log (7/17/19 2:16:15 PM)] TYPE "#DOWN."
[log (7/17/19 2:16:16 PM)] CLICK on L(44,234)@S(0)[0,0 1920x1200]
[log (7/17/19 2:16:16 PM)] TYPE "#DOWN."
[log (7/17/19 2:16:17 PM)] CLICK on L(44,234)@S(0)[0,0 1920x1200]
[log (7/17/19 2:16:17 PM)] TYPE "#DOWN."
[log (7/17/19 2:16:18 PM)] CLICK on L(44,234)@S(0)[0,0 1920x1200]
[log (7/17/19 2:16:19 PM)] TYPE "#DOWN."
[log (7/17/19 2:16:20 PM)] TYPE "#ENTER."

Am I doing something wrong here or is this a bug?

Question information

Language:
English Edit question
Status:
Solved
For:
Sikuli Edit question
Assignee:
No assignee Edit question
Solved by:
Manfred Hampl
Solved:
2019-07-17
Last query:
2019-07-17
Last reply:
2019-07-17
Best Manfred Hampl (m-hampl) said : #1

Looking at https://sikulix-2014.readthedocs.io/en/latest/keys.html#key-modifiers-modifier-keys you command seems the wrong way around:

the plain type() command has 2 parameters:
•parameter 1: the keys, that should be pressed/released one after the other
•parameter 2: the modifier keys, that alltogether should be pressed and held during the typing and released at the end.

So I thought it should be
type(Key.DOWN, Key.SHIFT)

If this doesn't work either, then I suggest trying a workaround:

     …
     type(startpoint) #click at an item in the subfolder - 7 in this case
     keyDown(Key.SHIFT)
     for __ in range(int(startpoint), int(endpoint)): #mark a bunch of items
         type(Key.DOWN)
         sleep(1)
     keyUp(Key.SHIFT)
     type(Key.ENTER)
     …

Singah (singah) said : #2

Thanks Manfred Hampl, that solved my question.

Singah (singah) said : #3

You're absolutely right, I got the order wrong of the command and it solved the issue with the click. Also, the second solution provided seems much more suitable for this case.

However, the call for the click() when giving incorrect parameters in type() seems really off and it seems that theres room for some proper error handling there.

RaiMan (raimund-hocke) said : #4

--- and it seems that theres room for some proper error handling there

accepted.

I will check, wether this
type(KeyModifier.SHIFT, Key.DOWN)

works at all and fix what is needed.