使用 Java 清除和编辑剪贴板
Clearing and editing Clipboard with Java
我在与系统剪贴板交互时遇到了一些问题(编辑、清除等)。
我有以下代码,当您构建它并双击 Jar 文件时,它会为您选择的不同站点打开多个浏览器 windows。
import java.awt.Robot;
import java.awt.event.KeyEvent;
import java.awt.datatransfer.*;
import java.awt.Toolkit;
import java.awt.datatransfer.DataFlavor;
import java.awt.datatransfer.StringSelection;
import java.awt.datatransfer.Transferable;
import java.awt.datatransfer.UnsupportedFlavorException;
//import clip.Clipboard.*;
public class main {
public static void main(String[] args) {
//Open Chrome
int keyControl = KeyEvent.VK_CONTROL;
int keyT = KeyEvent.VK_T;
// TODO Auto-generated method stub
try {
Process p = Runtime.getRuntime().exec("\"/Program Files (x86)/Google/Chrome/Application/chrome.exe\"");
p.waitFor();
System.out.println("Google Chrome launched!");
} catch (Exception e) {
e.printStackTrace();
}
try {
Robot robot = new Robot();
//Sites
String site = "www.youtube.com";
String site2 = "www.facebook.com";
//SITE 1 \\\\\\\\\\\\\\\\\\
//Load Url
StringSelection ss = new StringSelection(site);
Clipboard clpbrd = Toolkit.getDefaultToolkit().getSystemClipboard();
clpbrd.setContents(ss, null);
//Enter URL
robot.keyPress(KeyEvent.VK_CONTROL);
robot.keyPress(KeyEvent.VK_V);
robot.keyRelease(KeyEvent.VK_V);
robot.keyRelease(KeyEvent.VK_CONTROL);
robot.keyPress(KeyEvent.VK_ENTER);
robot.keyRelease(KeyEvent.VK_ENTER);
//Clear Clipboard
String empty = "";
StringSelection ss2 = new StringSelection(empty);
clpbrd = Toolkit.getDefaultToolkit().getSystemClipboard();
clpbrd.setContents(ss2, null);
//SITE 2 \\\\\\\\\\\\\\\\\\
//Load Url
StringSelection ss3 = new StringSelection(site2);
clpbrd = Toolkit.getDefaultToolkit().getSystemClipboard();
clpbrd.setContents(ss3, null);
//Create new tab
robot.keyPress(keyControl);
robot.keyPress(keyT);
robot.keyRelease(keyControl);
robot.keyRelease(keyT);
//Enter URL
robot.keyPress(KeyEvent.VK_CONTROL);
robot.keyPress(KeyEvent.VK_V);
robot.keyRelease(KeyEvent.VK_V);
robot.keyRelease(KeyEvent.VK_CONTROL);
robot.keyPress(KeyEvent.VK_ENTER);
robot.keyRelease(KeyEvent.VK_ENTER);
}
catch (Exception e) {
e.printStackTrace();
}
}
}
当我尝试 运行 这个程序时,它会打开选项卡,但每个选项卡都是相同的 link。所以它似乎是 运行 到剪贴板的问题。这是我第一次在代码中使用剪贴板。
我该如何进行这项工作?下载不了webdrivers/etc解决问题
我已经将您的代码稍微重构为更简洁的代码,而没有改变您在您的代码中所做的基本工作,因为您的代码对我来说效果很好。我只是想以此版本作为讨论的基础:
package de.scrum_master.Whosebug;
import java.awt.*;
import java.awt.datatransfer.Clipboard;
import java.awt.datatransfer.StringSelection;
import java.awt.event.KeyEvent;
import java.io.IOException;
import java.util.Arrays;
import java.util.List;
public class BrowserRemoteControl {
private static final Clipboard CLIPBOARD = Toolkit.getDefaultToolkit().getSystemClipboard();
private static final Robot ROBOT = createRobot();
public static void main(String[] args) throws IOException, InterruptedException, AWTException {
startChrome();
List<String> sites = Arrays.asList("youtube.com", "facebook.com");
for (String site : sites) {
setClipboardContent(site);
enterURLFromClipboard();
createNewBrowserTab();
}
}
private static Robot createRobot() {
try {
return new Robot();
} catch (AWTException e) {
throw new RuntimeException("Cannot create Robot instance", e);
}
}
private static void startChrome() throws IOException, InterruptedException {
Process process = Runtime.getRuntime().exec("\"C:/Program Files (x86)/Google/Chrome/Application/chrome.exe\"");
process.waitFor();
System.out.println("Google Chrome launched");
}
private static void setClipboardContent(String text) {
CLIPBOARD.setContents(new StringSelection(text), null);
}
private static void enterURLFromClipboard() throws InterruptedException {
// Ctrl-V
ROBOT.keyPress(KeyEvent.VK_CONTROL);
ROBOT.keyPress(KeyEvent.VK_V);
ROBOT.keyRelease(KeyEvent.VK_V);
ROBOT.keyRelease(KeyEvent.VK_CONTROL);
// Enter
ROBOT.keyPress(KeyEvent.VK_ENTER);
ROBOT.keyRelease(KeyEvent.VK_ENTER);
// Wait a little while
Thread.sleep(250);
}
private static void createNewBrowserTab() {
// Ctrl-T
ROBOT.keyPress(KeyEvent.VK_CONTROL);
ROBOT.keyPress(KeyEvent.VK_T);
ROBOT.keyRelease(KeyEvent.VK_CONTROL);
ROBOT.keyRelease(KeyEvent.VK_T);
}
}
希望您不介意最后多一个空的浏览器选项卡,但我想避免为最后一个站点创建例外。
因为你的代码对我有用,即使中间没有清理剪贴板,我猜你有某种计时问题或竞争条件。
更新: 我刚才成功重现了一次(而且只有一次)这个问题。所以我只是在粘贴 URL 并按 Enter 后添加了一个 Thread.sleep()
,以便向您展示如何在可能导致问题的所有地方执行相同的操作。
随时提出 follow-up 个问题。
我在与系统剪贴板交互时遇到了一些问题(编辑、清除等)。
我有以下代码,当您构建它并双击 Jar 文件时,它会为您选择的不同站点打开多个浏览器 windows。
import java.awt.Robot;
import java.awt.event.KeyEvent;
import java.awt.datatransfer.*;
import java.awt.Toolkit;
import java.awt.datatransfer.DataFlavor;
import java.awt.datatransfer.StringSelection;
import java.awt.datatransfer.Transferable;
import java.awt.datatransfer.UnsupportedFlavorException;
//import clip.Clipboard.*;
public class main {
public static void main(String[] args) {
//Open Chrome
int keyControl = KeyEvent.VK_CONTROL;
int keyT = KeyEvent.VK_T;
// TODO Auto-generated method stub
try {
Process p = Runtime.getRuntime().exec("\"/Program Files (x86)/Google/Chrome/Application/chrome.exe\"");
p.waitFor();
System.out.println("Google Chrome launched!");
} catch (Exception e) {
e.printStackTrace();
}
try {
Robot robot = new Robot();
//Sites
String site = "www.youtube.com";
String site2 = "www.facebook.com";
//SITE 1 \\\\\\\\\\\\\\\\\\
//Load Url
StringSelection ss = new StringSelection(site);
Clipboard clpbrd = Toolkit.getDefaultToolkit().getSystemClipboard();
clpbrd.setContents(ss, null);
//Enter URL
robot.keyPress(KeyEvent.VK_CONTROL);
robot.keyPress(KeyEvent.VK_V);
robot.keyRelease(KeyEvent.VK_V);
robot.keyRelease(KeyEvent.VK_CONTROL);
robot.keyPress(KeyEvent.VK_ENTER);
robot.keyRelease(KeyEvent.VK_ENTER);
//Clear Clipboard
String empty = "";
StringSelection ss2 = new StringSelection(empty);
clpbrd = Toolkit.getDefaultToolkit().getSystemClipboard();
clpbrd.setContents(ss2, null);
//SITE 2 \\\\\\\\\\\\\\\\\\
//Load Url
StringSelection ss3 = new StringSelection(site2);
clpbrd = Toolkit.getDefaultToolkit().getSystemClipboard();
clpbrd.setContents(ss3, null);
//Create new tab
robot.keyPress(keyControl);
robot.keyPress(keyT);
robot.keyRelease(keyControl);
robot.keyRelease(keyT);
//Enter URL
robot.keyPress(KeyEvent.VK_CONTROL);
robot.keyPress(KeyEvent.VK_V);
robot.keyRelease(KeyEvent.VK_V);
robot.keyRelease(KeyEvent.VK_CONTROL);
robot.keyPress(KeyEvent.VK_ENTER);
robot.keyRelease(KeyEvent.VK_ENTER);
}
catch (Exception e) {
e.printStackTrace();
}
}
}
当我尝试 运行 这个程序时,它会打开选项卡,但每个选项卡都是相同的 link。所以它似乎是 运行 到剪贴板的问题。这是我第一次在代码中使用剪贴板。
我该如何进行这项工作?下载不了webdrivers/etc解决问题
我已经将您的代码稍微重构为更简洁的代码,而没有改变您在您的代码中所做的基本工作,因为您的代码对我来说效果很好。我只是想以此版本作为讨论的基础:
package de.scrum_master.Whosebug;
import java.awt.*;
import java.awt.datatransfer.Clipboard;
import java.awt.datatransfer.StringSelection;
import java.awt.event.KeyEvent;
import java.io.IOException;
import java.util.Arrays;
import java.util.List;
public class BrowserRemoteControl {
private static final Clipboard CLIPBOARD = Toolkit.getDefaultToolkit().getSystemClipboard();
private static final Robot ROBOT = createRobot();
public static void main(String[] args) throws IOException, InterruptedException, AWTException {
startChrome();
List<String> sites = Arrays.asList("youtube.com", "facebook.com");
for (String site : sites) {
setClipboardContent(site);
enterURLFromClipboard();
createNewBrowserTab();
}
}
private static Robot createRobot() {
try {
return new Robot();
} catch (AWTException e) {
throw new RuntimeException("Cannot create Robot instance", e);
}
}
private static void startChrome() throws IOException, InterruptedException {
Process process = Runtime.getRuntime().exec("\"C:/Program Files (x86)/Google/Chrome/Application/chrome.exe\"");
process.waitFor();
System.out.println("Google Chrome launched");
}
private static void setClipboardContent(String text) {
CLIPBOARD.setContents(new StringSelection(text), null);
}
private static void enterURLFromClipboard() throws InterruptedException {
// Ctrl-V
ROBOT.keyPress(KeyEvent.VK_CONTROL);
ROBOT.keyPress(KeyEvent.VK_V);
ROBOT.keyRelease(KeyEvent.VK_V);
ROBOT.keyRelease(KeyEvent.VK_CONTROL);
// Enter
ROBOT.keyPress(KeyEvent.VK_ENTER);
ROBOT.keyRelease(KeyEvent.VK_ENTER);
// Wait a little while
Thread.sleep(250);
}
private static void createNewBrowserTab() {
// Ctrl-T
ROBOT.keyPress(KeyEvent.VK_CONTROL);
ROBOT.keyPress(KeyEvent.VK_T);
ROBOT.keyRelease(KeyEvent.VK_CONTROL);
ROBOT.keyRelease(KeyEvent.VK_T);
}
}
希望您不介意最后多一个空的浏览器选项卡,但我想避免为最后一个站点创建例外。
因为你的代码对我有用,即使中间没有清理剪贴板,我猜你有某种计时问题或竞争条件。
更新: 我刚才成功重现了一次(而且只有一次)这个问题。所以我只是在粘贴 URL 并按 Enter 后添加了一个 Thread.sleep()
,以便向您展示如何在可能导致问题的所有地方执行相同的操作。
随时提出 follow-up 个问题。