1

Basically, i've selected some text and trying to copy it; but below code is not doing Ctrl+C during runtime-nothing is storing into clipboard-however manually I've tried my keys are working fine, but it used to work a few days back. Could you please help me on this issue.

import java.awt.AWTException;
import java.awt.HeadlessException;
import java.awt.Robot;
import java.awt.Toolkit;
import java.awt.datatransfer.DataFlavor;
import java.awt.datatransfer.UnsupportedFlavorException;
import java.awt.event.KeyEvent;
import java.io.IOException;

public class Robot_CopyPaste{

public static void main(String[] args) throws AWTException, InterruptedException, HeadlessException, UnsupportedFlavorException, IOException{

        Robot robot=new Robot();
        Thread.sleep(4000);

        robot.keyPress(KeyEvent.VK_SHIFT);
        robot.setAutoDelay(50);
        robot.keyPress(KeyEvent.VK_RIGHT);
        robot.setAutoDelay(50);
        robot.keyPress(KeyEvent.VK_RIGHT);
        robot.setAutoDelay(50);
        robot.keyPress(KeyEvent.VK_RIGHT);
        robot.setAutoDelay(50);
        robot.keyPress(KeyEvent.VK_RIGHT);

        robot.setAutoDelay(50);
        robot.keyRelease(KeyEvent.VK_RIGHT);
        robot.setAutoDelay(50);
        robot.keyRelease(KeyEvent.VK_SHIFT);
        robot.setAutoDelay(50);
        System.out.println("Check1");

        robot.keyPress(KeyEvent.VK_CONTROL);
        robot.setAutoDelay(50);;
        robot.keyPress(KeyEvent.VK_C);
        robot.setAutoDelay(50);
        robot.keyRelease(KeyEvent.VK_C);
        robot.setAutoDelay(50);
        robot.keyRelease(KeyEvent.VK_CONTROL);
        robot.setAutoDelay(50);
        Thread.sleep(4000);
        System.out.println("Check2");
        Thread.sleep(2000);
    String str=(String)Toolkit.getDefaultToolkit().getSystemClipboard().getData(DataFlavor.stringFlavor);   
        Thread.sleep(2000);
        System.out.println("Copied content is :"+str);
    }
}

This code is to enter Input in CAPS by holding SHIFT Please check code and let me know I think issue is with Shift + Right Arrow----

 import java.awt.AWTException;
 import java.awt.Robot;
 import java.awt.event.KeyEvent;

public class Robot_SendKeys {
static void sendKeys(Robot robot, String keys) {
    for (char c : keys.toCharArray()) {
        int keyCode = KeyEvent.getExtendedKeyCodeForChar(c);
        if (KeyEvent.CHAR_UNDEFINED == keyCode) {
            throw new RuntimeException(
                "Key code not found for character '" + c + "'");
        }
        System.out.println(keyCode);
        robot.keyPress(keyCode);
        robot.delay(100);
        robot.keyRelease(keyCode);
        robot.delay(100);
    }
}
public static void main(String[] args) throws AWTException {
    Robot robot = new Robot();
    robot.setAutoDelay(50);
    robot.keyPress(KeyEvent.VK_SHIFT);
    sendKeys(robot, "Test");
    robot.keyRelease(KeyEvent.VK_SHIFT);
}

}

luator
  • 4,769
  • 3
  • 30
  • 51
Praveen Reddy
  • 11
  • 1
  • 4
  • Using java.awt.Robot, your code works for me. Can you post the complete code? Also note that using `robot.setAutoDelay(50);` may be easier – phflack Jan 17 '18 at 17:07
  • Why are you sending CTRL+C to copy something into the clipboard? There are Java APIs to put data into the clipboard that should be a lot more reliable. See: https://stackoverflow.com/questions/6710350/copying-text-to-the-clipboard-using-java – JeffC Jan 17 '18 at 17:16
  • @JeffC You assume too much. In many cases the text being copied is not in the application, but may be on a web page – phflack Jan 17 '18 at 17:39
  • @phflack I'm confused why you think that's any different? If it's on the webpage, you scrape the page to get the text and then use the Java API (instead of Robot CTRL+C) to push that text into the clipboard. It's a much more reliable way to handle these situations. – JeffC Jan 17 '18 at 18:00
  • @JeffC It is not always simple to scrape a web page or application, but may be easier to select the text with the robot, and then copy to clipboard – phflack Jan 17 '18 at 18:02
  • @phflack You realize that makes no sense, right? You are going to pass a locator to Robot to click in a field and then select all and then CTRL+C vs just using `.getText()` or get the value of an input. It's not that much different except that Robot is less reliable (or this question wouldn't exist). – JeffC Jan 17 '18 at 18:05
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/163346/discussion-between-phflack-and-jeffc). – phflack Jan 17 '18 at 18:05
  • @phflack thanks for looking into this. Jeff, I'm not using any Web based application. I've to copy text from any Desktop based application, in my case mainframe screen-i'll select some part of text and need to copy-which should store into clipboard but it's not working for me. Please find complete code updated. Before running code just point your cursor on the first line (1:1) in java class and run. It will select first 4 characters and Copy. Expected output is: impo – Praveen Reddy Jan 18 '18 at 02:44
  • @phflack In the above code-i've given sample, that is similar to my requirement, if we place cursor at (1:1) position and run this file, it should select first 4 characters in the java file and Copy. However it's selecting but not copying, it used to work perfectly few days back. Please check and help me on this. Let me know if you need any other details. Thanks in Advance – Praveen Reddy Jan 18 '18 at 02:55
  • I think after entering the text , it will store as the value in one of the attribute right? , so you should be able to get the entered value by using getAttribute method i feel – Pradeep hebbar Jan 18 '18 at 12:43
  • @Pradeephebbar No Pradeep, that's for web-based application, but here my requirement for Desktop based app. I'm supposed to use only robot class and work. So getAttribute is not possible. – Praveen Reddy Jan 18 '18 at 13:33
  • A. You only need to use `robot.setAutoDelay(50);` once, before pressing keys. B. It looks like it's having trouble with pressing shift – phflack Jan 18 '18 at 14:28
  • @phflack you are right, the problem is with pressing shift+right arrow together not working or selecting text, but manually works fine, I'm updating subject line of the issue accordingly. May I know who can help me on this? Thanks – Praveen Reddy Jan 19 '18 at 07:22
  • Will look into it, while testing it looked like it was using the correct key code (it undid me holding shift), didn't test if it would type capital letters to verify though – phflack Jan 19 '18 at 07:25
  • @phflack I've just checked that SHIFT+entering text. It' s working fine. Updated the code in the body. I'm confused what exactly issue is? – Praveen Reddy Jan 19 '18 at 07:45
  • Potentially as a workaround until I can find a better solution: use `CTRL+A` to select everything, then copy and parse the result – phflack Jan 19 '18 at 21:10
  • @phflack Any update I can expect from you? or Let me know who can look into this quickly? – Praveen Reddy Feb 08 '18 at 10:24

1 Answers1

0

I found the solution in a 2003 bug report LOL

To be able to press shift+arrow using robot you need to disable NumLock, it actually worked for me. I am currently working with a really old mainframe screen so this is actually extremely useful. I hope this is helpful.

https://bugs.java.com/bugdatabase/view_bug.do?bug_id=4909137