0

I was try to make automation software that can paste some copied data from clipboard, but get some weird behavior. I use java.awt.Robot to achieve this. This is the code :

try{
    java.awt.Robot robot = new Robot();
    robot.setAutoDelay(250);
    robot.keyPress(KeyEvent.VK_CONTROL);
    robot.keyPress(KeyEvent.VK_V);
    robot.keyRelease(KeyEvent.VK_V);
    robot.keyRelease(KeyEvent.VK_CONTROL);
}catch(AWTException ex){
    ex.printStackTrace();
}

How to trigger native paste event properly on Windows with Java?

luator
  • 4,769
  • 3
  • 30
  • 51
NM Naufaldo
  • 1,032
  • 1
  • 12
  • 30

1 Answers1

0

I just solved the problem and the paste event work as expected. Because my project using JavaFX, there is 2 solution here using java.awt.Robot

try{
    java.awt.Robot r = new Robot();
    r.keyPress(KeyEvent.VK_CONTROL);
    r.keyPress(KeyEvent.VK_V);
    r.keyRelease(KeyEvent.VK_CONTROL);
    r.keyRelease(KeyEvent.VK_V);
}catch(AWTException e){
    e.printStackTrace();
}

or javafx.scene.robot.Robot

javafx.scene.robot.Robot r = new javafx.scene.robot.Robot();
r.keyPress(KeyCode.CONTROL);
r.keyPress(KeyCode.V);
r.keyRelease(KeyCode.CONTROL);
r.keyRelease(KeyCode.V);

I choose using javafx.scene.robot.Robot, because in some situation java.awt.Robot not work properly

NM Naufaldo
  • 1,032
  • 1
  • 12
  • 30