gpt4 book ai didi

java - 来自剪贴板类型的机器人粘贴 "v"

转载 作者:太空宇宙 更新时间:2023-11-04 10:40:01 25 4
gpt4 key购买 nike

(OSX) 我正在尝试粘贴字符串“Hello World!”从我的剪贴板到一个单独的应用程序。字符串被复制到我的剪贴板,我可以手动粘贴它,但机器人只键入“v”,而不是同时按下 VK_META + VK_V 键。

public static void sendString() throws Exception {
Thread.sleep(3000);
String text = "Hello World!";
StringSelection stringSelection = new StringSelection(text);
Clipboard clipboard = Toolkit.getDefaultToolkit().getSystemClipboard();
clipboard.setContents(stringSelection, stringSelection);
try {
Robot robot = new Robot();
robot.keyPress(KeyEvent.VK_META);
robot.keyPress(KeyEvent.VK_V);
robot.keyRelease(KeyEvent.VK_V);
robot.keyRelease(KeyEvent.VK_META);
} catch(java.awt.AWTException awte) {
System.out.println("AWTException");
}
}

最佳答案

为了确保命令的顺序,请在命令之间添加延迟:

try{
Robot robot = new Robot();
robot.keyPress(KeyEvent.VK_META);
robot.delay(500); // 200~500
robot.keyPress(KeyEvent.VK_V);
robot.keyRelease(KeyEvent.VK_V);
robot.keyRelease(KeyEvent.VK_META);
} catch(java.awt.AWTException awte) {
System.out.println("AWTException");
}

关于java - 来自剪贴板类型的机器人粘贴 "v",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49093980/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com