gpt4 book ai didi

java - 如何使用java.awt.Robot发送包含大写字母和小写字母以及特殊字符的字符串?

转载 作者:行者123 更新时间:2023-12-01 23:50:55 24 4
gpt4 key购买 nike

假设我想发送下面的字符串

String 1 : cd /srcdir/data/PTcpGateway

String 2 : vi am1py_packets_PS.config

我不想一一发送字符,而是想一次性发送整个字符串。可以使用Java机器人吗?

我试图关注this帖子,但它对我不起作用。

最佳答案

对于腻子自动化,我相信可以工作的代码应该在下面,考虑到它在大型机应用程序界面上对我有用。它或多或少类似于预计的解决方案。

代码片段 1:-

public static void typeAction (String text, int waitAfterKeyPressRelease) throws InterruptedException, AWTException  {

// Performs Copy Paste of chain of keys ... i.e. word ( including special chars )
StringSelection stringSelection = new StringSelection(text);
Clipboard clipboard = Toolkit.getDefaultToolkit().getSystemClipboard();
clipboard.setContents(stringSelection, stringSelection);

Thread.sleep(2000);
Robot robot = new Robot();
robot.keyPress(KeyEvent.VK_CONTROL);
robot.keyPress(KeyEvent.VK_V);
robot.keyRelease(KeyEvent.VK_V);
robot.keyRelease(KeyEvent.VK_CONTROL);

}

片段 2:

public static void typeAction (char[] text, int waitAfterKeyPressRelease) throws InterruptedException, AWTException  {

for (char c : text) {
int keyCode = KeyEvent.getExtendedKeyCodeForChar(c);
if (KeyEvent.CHAR_UNDEFINED == keyCode || keyCode == KeyEvent.VK_UNDEFINED )
throw new RuntimeException("Key code not found for character '" + c + "'");
robot.keyPress(keyCode);
robot.delay(10);
robot.keyRelease(keyCode);
robot.delay(10);
}
}

片段 3:

public static void typeAction( int key, int waitAfterKeyPressRelease) {


try{
robot = new Robot();
robot.keyPress(key);
if(waitAfterKeyPressRelease>0) Thread.sleep(waitAfterKeyPressRelease);
robot.keyRelease(key);
if(waitAfterKeyPressRelease>0) Thread.sleep(waitAfterKeyPressRelease);
}

catch ( AWTException awt) { awt.printStackTrace(); }
catch (InterruptedException Int) { Int.printStackTrace(); }

}

关于java - 如何使用java.awt.Robot发送包含大写字母和小写字母以及特殊字符的字符串?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58220899/

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