javakeypressawtrobotkeyrelease

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);

Solution

  • 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.