我正在运行以下代码:
String clipBoardText;
public String getAllTextOfPage() {
StringSelection stringSelection = new StringSelection(null);
Toolkit.getDefaultToolkit().getSystemClipboard().setContents(
stringSelection, null);
Robot r = null;
try {
r = new Robot();
} catch (AWTException e) {
e.printStackTrace();
}
r.keyPress(KeyEvent.VK_CONTROL);
r.keyPress(KeyEvent.VK_A);
r.keyRelease(KeyEvent.VK_A);
try {
Thread.sleep(1000);
}
catch (InterruptedException ie) {
System.out.print(ie.getLocalizedMessage());
}
r.keyPress(KeyEvent.VK_C);
r.keyRelease(KeyEvent.VK_CONTROL);
r.keyRelease(KeyEvent.VK_C);
Clipboard clipboard = Toolkit.getDefaultToolkit().getSystemClipboard();
try {
clipBoardText = (String)clipboard.getContents(null).getTransferData(DataFlavor.stringFlavor);
System.out.println(clipBoardText);
}
catch (UnsupportedFlavorException ex){
System.out.println(ex);
ex.printStackTrace();
}
catch (IOException ex) {
System.out.println(ex);
ex.printStackTrace();
}
return clipBoardText;
}
控制台输出如下:
null
Process finished with exit code 0
但是当我将剪贴板的内容粘贴到记事本中时,它有我正在寻找的内容。因此,我在检索剪贴板内容时似乎存在问题。