问题描述
我有一个程序,该程序打开一个程序,然后将其复制并粘贴到程序中,并在一段时间后使用ctrl + c机器人将其从程序中复制到剪贴板。然后,我的程序检查复制的字符串是否包含单词,但不是检查最近复制的字符串,而是使用开头的先前复制的字符串。这里是一些代码:
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
}
system.out东西只输出String1的值,而不是string2的值。
The system.out thing outputs just the value of String1 not the value string2. Thanks for your help.
推荐答案
假设您的copy()方法设置了剪贴板文本,我遇到了完全相同的问题,并且找到了一个怪异的解决方法。
Assuming that your copy() method sets the clipboard text, I am having the exact same problem and have found a weird workaround.
我可以获取剪贴板上的文本,该文本将始终显示用户在任何时间复制的内容,但是如果我以编程方式设置剪贴板上的文本,那么这就是我从剪贴板上获得的内容-除了如果我将通过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).
这篇关于java剪贴板错误(错误)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!