Performance for keyboard simulation under macOS (Java SE 15.0.2) differs heavily from Win10. Typing '100 a' takes on Win10 (openJDK 9.0.4, 14.0.2, 15.0.2) 140ms, Ubuntu VM (openJDK 11.0.9.1) 8200ms and on macOS 11000ms. Results are same on macOS Catalina VM and macOS BigSur on MacBook Pro 2020. Ubuntu test takes 2300ms, if just set focus to text editor direct after start program from console (no effect on macOS).
Inserting robot.delay(30) after each keyPress/keyRelease didn't achieved performance gains on macOS. Does anyone can confirm this behavior? It seems like a performance issue in macOS implementation.
When using javafx.scene.robot.Robot time on Win10 and MacOS is 30ms.
package JavaRobotTest;
import java.awt.*;
import java.awt.event.KeyEvent;
public class sample {
public static void main(String[] args) {
StringBuilder sb = new StringBuilder();
for (Integer i = 0; i < 100; i++) {
sb.append('a');
}
typeText(sb.toString());
}
private static void typeText(String text) {
try {
// Win10 ~140ms, macOS ~11000ms
Robot robot = new Robot();
new Thread(() -> {
// Commenting this line result in Win10 ~15ms, macOS ~11000ms
robot.setAutoWaitForIdle(true);
typeTextRobot(robot, text);
}).start();
} catch (AWTException e) {
e.printStackTrace();
}
}
private static void typeTextRobot(Robot robot, String text) {
long start = System.currentTimeMillis();
for (char c : text.toCharArray()) {
Integer key = KeyEvent.VK_A;
try {
robot.keyPress(key);
//robot.delay(30);
robot.keyRelease(key);
//robot.delay(30);
} catch (Exception e) {
e.printStackTrace();
}
}
System.out.println("DURA ms: " + (System.currentTimeMillis() - start));
}
}