java Clipboard error (bug)?

2019-04-16 13:27发布

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.

1条回答
Explosion°爆炸
2楼-- · 2019-04-16 13:53

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).

查看更多
登录 后发表回答