0

Suppose I want to send the string below

String 1 : cd /srcdir/data/PTcpGateway

String 2 : vi am1py_packets_PS.config

I don't wanted to send one by one character, instead want to send whole string in one go. Is it possible using Java Robot?

I tried to follow this post, but it does not work for me.

Shoaib Akhtar
  • 1,393
  • 5
  • 17
  • 46
  • When you followed the other post what did not work for you? What errors did you get? Please supply the code you tried and any errors/results. – mwarren Oct 03 '19 at 14:16
  • What do you mean by "send"? Why do you think you need to use a Robot if you already know what the String is? – camickr Oct 03 '19 at 14:19
  • @camickr I want to send such string as command in putty automation – Shoaib Akhtar Oct 03 '19 at 14:30
  • I don't know what "putty automation" is. In any case, the Robot class can only send one character at a time. So you need to create a method to build the String and have the Robot send 1 character at a time from the String. Its like when you type on a keyboard you can only type one character at a time. There is no copy/paste functionality for a Robot. – camickr Oct 03 '19 at 14:39

1 Answers1

0

For putty automation I believe the code that would work should be below, considering it worked for me on mainframe application interface. It's more or less similar to projected solutions.

Snippet 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);

}

Snippet 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);
     }
}

Snippet 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();  }

}
Maddy
  • 29
  • 6