How to simulate key pressed from Java project?

Asked by Mathieu Bordas

I use sikuli-script.jar in my project, running on Windows and Mac (a lot of automated tests).
I would like to simulate a key pressed, like 'ENTER', 'FIRST', or 'PGUP' for example. I haven't seen how to do that.
I cannot do that with methods type() and paste() from the Region class.

Question information

Language:
English Edit question
Status:
Solved
For:
SikuliX Edit question
Assignee:
No assignee Edit question
Solved by:
Mathieu Bordas
Solved:
Last query:
Last reply:
Revision history for this message
RaiMan (raimund-hocke) said :
#1

running your tests on the java level?

Revision history for this message
Mathieu Bordas (mat-bordas) said :
#2

Yes, my tests are written in Java, in a full Java project, and I use sikuli-script.jar classes, mainly Screen and Region.

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

Using type() is only documented on the Python level.

the principle usage in Java is (as you might already know) (if the cursor is already positioned by a click() before at the input field):

Screen.type(null, "characters to be typed", KEY_MOD)

Where KEY_MOD is an integer representing a combination of ALT, SHIFT, CTRL, WIN/CMD/META - if 0, no modifier keys.

Special keys have to be incorporated into the string using some unicode constants.

The easiest way to get into type() on Java level is to look into the code of Region.java at:
http://bazaar.launchpad.net/%7Esikuli-driver/sikuli/sikuli-x/annotate/head%3A/sikuli-script/src/main/java/org/sikuli/script/Region.java

The unicode table can be found near the bottom.

Revision history for this message
Mathieu Bordas (mat-bordas) said :
#4

Thanks a lot. But calling type() with a null pattern does not work for me (sikuli version X): nothing is done. That was one reason why I looked for a different method. I will try to re-code my own type() method without pattern image.

Something else:
when I try to type the '.' character, this Exception is thrown:
[debug] type "del?.", mod: (0)
java.lang.IllegalArgumentException: Invalid key code
 at sun.awt.windows.WRobotPeer.keyPress(Native Method)
 at java.awt.Robot.keyPress(Robot.java:243)
 at org.sikuli.script.Region.doType(Region.java:745)
[...]

my code is:
type("quickSearchBar.png", "my search" + '.');

I have no idea how to simulate the keyboard pressing '.' key. It also appears with other keys, such as DELETE.

Note that I work with a french desktop (azerty keyboard), the '.' character correponds to SHIFT + ';' on my keyboard.

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

when using type() with a null pattern, there has to be a click() before to activate.
type() with a pattern internally does a click() before typing. So it is your choice.

Type with characters from the keyboard only works with the US qwerty layout.

Again: look at the code into the table, to find out, what unicode characters have to be given, to get what you want.

Revision history for this message
Mathieu Bordas (mat-bordas) said :
#6

Thanks a lot RaiMan.