user12314100
user12314100

Reputation:

Java how to write _ with Robot class

I use the robot class in my Java Code. I want to press the key "_" but the keycode 189 doesn't work for the Code

public void type() throws Exception

Robot rob = new Robot();

rob.keyPress(189);
rob.keyRelease(189);

Upvotes: 0

Views: 1478

Answers (2)

XtremeBaumer
XtremeBaumer

Reputation: 6435

The proper way is to use SHIFT + -.

Robot rob = new Robot();
rob.keyPress(KeyEvent.VK_SHIFT);
rob.keyPress(KeyEvent.VK_MINUS);
rob.keyRelease(KeyEvent.VK_MINUS);
rob.keyRelease(KeyEvent.VK_SHIFT);

It seems that on a French keyboard following code would work, as the keyboard as a designated underscore key:

Robot rob = new Robot();
rob.keyPress(KeyEvent.VK_UNDERSCORE);
rob.keyRelease(KeyEvent.VK_UNDERSCORE);

From this source they say:

Numbers are not prioritized

Writing the numbers on a French keyboard requires using the shift key each time.

That means the AZERTY keyboard prioritizes things like the accented letters (such as é) and brackets - and even the ampersand (&) over numbers.

enter image description here

Looking at the 8 key, you can find the underscore.

Upvotes: 4

SteffenJacobs
SteffenJacobs

Reputation: 412

This is caused by the keyboard layout not having an actual underscore key. To get an underscore, you have to press Shift + - (minus).

rob.keyPress(KeyEvent.VK_SHIFT);
rob.keyPress(KeyEvent.VK_MINUS);
rob.keyRelease(KeyEvent.VK_MINUS);
rob.keyRelease(KeyEvent.VK_SHIFT);

Source

Upvotes: 0

Related Questions