Java 如何用 Robot 写 _ class

Java how to write _ with Robot class

我在 Java 代码中使用机器人 class。我想按“_”键,但键码 189 不适用于代码

public void type() throws Exception

Robot rob = new Robot();

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

这是由于键盘布局没有实际的下划线键造成的。要获得下划线,您必须按 Shift + -(减号)。

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

Source

正确的方法是使用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);

似乎在法语键盘上以下代码可以工作,因为键盘是指定的下划线键:

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

他们从 this 来源说:

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.

查看8键,可以找到下划线