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

问题描述 投票:0回答:1

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

String 1 : cd /srcdir/data/PTcpGateway

String 2 : vi am1py_packets_PS.config

我不想一一发送,而是一口气发送整个字符串。是否可以使用Java Robot?

我尝试关注this帖子,但对我而言不起作用。

java swing awtrobot
1个回答
0
投票

对于油灰自动化,我认为可以工作的代码应该在下面,因为它在大型机应用程序界面上对我有用。它或多或少与计划的解决方案相似。

代码段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();  }

}
© www.soinside.com 2019 - 2024. All rights reserved.