I have a program, which opens a program, then it copies and pastes a string into the program and after some time it copies a string from the program to the clipboard using a robot with ctrl+c. Then my program checks if the copied string contains a word, but instead of inspecting the recently copied string it uses the previous copied string of the beginning. Heres some code:
new ProcessBuilder("pathToProgram").start();
copy(STRING1);
paste();
Thread.sleep(x);
//Move mouse to a position
//robot uses ctrl+a
copy();
Thread.sleep(100);
clipboardData = (String) Toolkit.getDefaultToolkit().getSystemClipboard().getData(DataFlavor.stringFlavor);
system.out.println(clipboardData);
if(clipboardData.contains(String2){
//do some stuff
}
The system.out thing outputs just the value of String1 not the value string2. Thanks for your help.
Assuming that your copy() method sets the clipboard text, I am having the exact same problem and have found a weird workaround.
Java clipboard ignores user copy if not SwingUtilities.invokeLater()
I can get the clipboard text, which will always show whatever the user has copied there at any time. But if I set the clipboard text programmatically, afterwards that's all I get from the clipboard - except if I postpone further clipboard text getting via SwingUtilities.invokeLater() one time, then the clipboard text getting works fine again. I can then also break it again - and "fix" it again.
I have no idea why this is so, hence the question I just posted (see link).