假设我想在下面发送字符串
String 1 : cd /srcdir/data/PTcpGateway
String 2 : vi am1py_packets_PS.config
我不想一一发送,而是一口气发送整个字符串。是否可以使用Java Robot?
我尝试关注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(); }
}