How do I effect a left arrow key press with the type command?

Asked by Lance Jensen

I have used Sikuli to effect some automation and am generally happy. I would be more satisfied if I could do some other things like perform left arrow keypresses to move to the beginning of a text box. I would also like to be able to do thinks like copy and paste. I understand how to do a keyDown and keyUp command but I am not sure of the semantics on specifying the keys of interest. Can you point me to a manual or give me the key press codes for left arrow and Ctrl and other non-text keys?

Do I simply use the Java virtual keys like KeyEvent.VK_SHIFT... If so, where is the listing of keypresses for the keys mentioned above.

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
Lance Jensen (lance-ti) said :
#1

Additional comment: It appears that there was documentation for the Key class at:

       http://sikuli.org/documentation.shtml#doc/pythondoc-python.edu.mit.csail.uid.Sikuli.html

But since the time of last posting this has fallen into disrepair and is no longer functional. Please give me the definition of the Key object. I think this will be sufficient for me.

Revision history for this message
Lance Jensen (lance-ti) said :
#2

Finally found it in your documentation:

12.2. Special Keys ¶

The methods supporting the use of special keys are type(), keyDown(), and keyUp().

Usage: Key.CONSTANT (where CONSTANT is one of the following key names). Concatenation with strings with "+" can be used.

    * miscellanous keys

    ENTER - TAB - ESC - BACKSPACE - DELETE - INSERT

    * function keys

    F1 - F2 - F3 - F4 - F5 - F6 - F7 - F8 - F9 - F10 - F11 - F12 - F13 - F14 - F15

    * navigation keys

    HOME - END - LEFT - RIGHT - DOWN - UP - PAGE_DOWN - PAGE_UP

    * key modifiers

    ALT - CMD - CTRL - META - SHIFT - WIN

So to do a copy for example would be type("c" , Key.CTRL) ?

Revision history for this message
Best RaiMan (raimund-hocke) said :
#3

No, it is:
type("c", KEY_CTRL)

The Key.XXXX are only special keys, that can be used in the first operand of type() (pressing keys = down-up). The second parameter is the so called key modifier (one or more of the keys Shift, Alt and Ctrl on Windows), which are "held down" during the typing of all the characters in the first parameter (written as KEY_SHIFT, KEY_ALT, KEY_CTRL and concatenated with +, if more than one is needed)

Tip: if you don't have the standard en-US keyboard layout, use paste() instead of type() for typing text, especially if it contains special characters like @, \ and others.

Getting text to the clipboard has generally to be done with something like Ctrl-c or menu functions of running apps after having it selected by means of Sikuli (mouse actions like dragDrop() ...). Once you have something on the clipboard you can have it in your script as text using Env.getClipboard().

Revision history for this message
Lance Jensen (lance-ti) said :
#4

Thanks for the prompt support. The ability to do complex key strokes was very helpful. Thanks again.

Revision history for this message
emrah kara (emrah-karakoc) said :
#5

how can i do ctrl+c or ctrl+v with java ?

screen.paste("c",KEY_CTRL). it doesnt work?
Do you have any idea?

Revision history for this message
RaiMan (raimund-hocke) said :
#6

@ emrah
When asking questions in a comment of a question, you have to subscribe to that question (did it for you ;-), if you want to get notified about reactions.

see faq 1444

Revision history for this message
Lance Jensen (lance-ti) said :
#7

The java class you are looking for is java.awt.Robot. Once you have made a robot object you can do key presses and mouse moves/clicks. Sulkui is great for some things but eventually I broke down and wrote custom code most of my automation. One thing to watch out for is system lag. Silkui is fairly resistant to system lag as the image recognition functions as an interrupt handler with timeout. Hope this helps.

As for how to do a copy and paste with the Robot class:
// Get robot class
GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
GraphicsDevice[] gs = ge.getScreenDevices();
myRobot = new Robot(gs[0]);

// do copy opperation
myRobot.delay(50);
myRobot.keyPress(KeyEvent.VK_CONTROL);
type("c"); // type methods are below
myRobot.delay(50);
myRobot.keyRelease(KeyEvent.VK_CONTROL);

// do paste opperation
myRobot.delay(50);
myRobot.keyPress(KeyEvent.VK_CONTROL);
type("c"); // type methods are below
myRobot.delay(50);
myRobot.keyRelease(KeyEvent.VK_CONTROL);

private void type(String Text) {
            char[] chars = Text.toCharArray();
            int code;
            for (int i = 0; i < chars.length; i++) {
                // Parse the first character
                typeKeyCode(chars[i]);
            }
}

private void typeKeyCode(int c) {
            myRobot.keyPress(c);
            myRobot.delay(50);
            myRobot.keyRelease(c);
}

Revision history for this message
RaiMan (raimund-hocke) said :
#8

@ Lance

this is, how Region.type() is realized internally in Sikuli.

On top it allows, to integrate special characters (e.g. Key.DOWN) into a string to be typed by using the unicode characters as documented in faq 1444.