Why simulating Control_A with Robot in Java is not working?
Question
I use the following lines to simulate a Control_A [ select all ] key action in Java with robot, but the clipboard is not getting the text, why ?
Robot robot=null;
try { robot=new Robot(); }
catch (AWTException ex) { System.err.println("Can't start Robot: " + ex); }
robot.mouseMove(260,500);
robot.mousePress(InputEvent.BUTTON1_MASK);
// robot.mouseMove(660,700);
robot.mouseRelease(InputEvent.BUTTON1_MASK);
robot.keyPress(KeyEvent.VK_CONTROL); // Select all
robot.keyPress(KeyEvent.VK_A);
robot.keyRelease(KeyEvent.VK_A);
robot.keyRelease(KeyEvent.VK_CONTROL);
robot.keyPress(KeyEvent.VK_CONTROL); // Copy
robot.keyPress(KeyEvent.VK_C);
robot.keyRelease(KeyEvent.VK_C);
robot.keyRelease(KeyEvent.VK_CONTROL);
Transferable t=Toolkit.getDefaultToolkit().getSystemClipboard().getContents(null);
try
{
if (t!=null && t.isDataFlavorSupported(DataFlavor.stringFlavor))
{
String text=(String)t.getTransferData(DataFlavor.stringFlavor);
System.out.println(text);
}
}
catch (Exception ex) { ex.printStackTrace(); }
I have a browser open so at [260,500] on screen there is text at that area. What have I missed ?
Edit:
I just found something strange, when I opened a browser, the text in the browser is not copies, but if I open a notepad/wordpad, the text in them will be copies, so why the browser didn't do it ?
Solution
All of your code should be inside the
try
block where you instantiate theRobot
because you could end up trying to work with a null reference and get aNullPointerException
. And if yourRobot
never got created and never copied contents, there'd be no point in you trying to access the contents from the clipboard either.I'm not entirely sure why but adding a small delay before trying to read from the clipboard fixes things. I'm guessing it might have to do with a race condition between Java getting hold of the clipboard before the system has had time to update it.
This updated code should work:
Robot robot = null;
try
{
robot = new Robot();
robot.mouseMove(260, 500);
robot.mousePress(InputEvent.BUTTON1_MASK);
// robot.mouseMove(660,700);
robot.mouseRelease(InputEvent.BUTTON1_MASK);
robot.keyPress(KeyEvent.VK_CONTROL); // Select all
robot.keyPress(KeyEvent.VK_A);
robot.keyRelease(KeyEvent.VK_A);
robot.keyRelease(KeyEvent.VK_CONTROL);
robot.keyPress(KeyEvent.VK_CONTROL); // Copy
robot.keyPress(KeyEvent.VK_C);
robot.keyRelease(KeyEvent.VK_C);
robot.keyRelease(KeyEvent.VK_CONTROL);
try
{
//sleep just a little to let the clipboard contents get updated
Thread.sleep(25);
}
catch (InterruptedException e)
{
e.printStackTrace();
}
Transferable t = Toolkit.getDefaultToolkit().getSystemClipboard().getContents(null);
try
{
if (t != null && t.isDataFlavorSupported(DataFlavor.stringFlavor))
{
String text = (String) t.getTransferData(DataFlavor.stringFlavor);
System.out.println(text);
}
}
catch (Exception ex)
{
ex.printStackTrace();
}
}
catch (AWTException ex)
{
System.err.println("Can't start Robot: " + ex);
}