Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
251 views
in Technique[技术] by (71.8m points)

awtrobot - How can I select text in another application with java.awt.robot (example: shift+home)

I am trying to use java.awt.robot to fix some Excel arrays for me automatically. In order to do so, I need to select the text inside a cell. It seems that shift+home and shift+arrow keys do not work for selecting text. Here is a code snippet:

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

public class Fix4DArray {

public static void main(String[] args){
    int n = 10; //nominal sleep time
    try {
        Robot r = new Robot();
        Sequence(r,n,KeyEvent.VK_ALT,KeyEvent.VK_TAB); //switch windows to Excel
        Thread.sleep(200);
        Press(r,n,KeyEvent.VK_F2); //Open Excel editing for currently selected cell
        Select(r,n,KeyEvent.VK_SHIFT,KeyEvent.VK_HOME); //Select all text in cell DOES NOT WORK
        //Select(r,n,KeyEvent.VK_SHIFT,KeyEvent.VK_LEFT); //Select one character DOES NOT WORK
    }       
    catch (Exception e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
}



public static void Press(Robot r,int sleep, int keyEvent) throws InterruptedException{
    r.keyPress(keyEvent);
    r.keyRelease(keyEvent);
    Thread.sleep(sleep);
}

public static void Select(Robot r, int sleep, int... keyEvents ) throws InterruptedException{
    r.keyPress(keyEvents[0]);       
    for(int i = 1; i < keyEvents.length; i++){
        r.keyPress(keyEvents[i]);
        Thread.sleep(sleep);
    }
    r.keyRelease(keyEvents[0]);

}

public static void Sequence(Robot r, int sleep, int... keyEvents ) throws InterruptedException{
    for(int i = 0; i < keyEvents.length; i++){
        r.keyPress(keyEvents[i]);
        Thread.sleep(sleep);
    }
    for(int i = keyEvents.length; i > 0 ; i--){
        r.keyRelease(keyEvents[i-1]);
        Thread.sleep(sleep);
    }
}
}

Note that if you comment out the alt+tab (line 12) at the beginning and run from Eclipse (or another IDE), it will not select the text in Eclipse either, although it will move the cursor. CTRL-A does work, but won't select the text in Excel, so it'a a no-go.

I found one reference here https://community.oracle.com/thread/1289981?start=0&tstart=0 about this subject, but they did not come to a solution.

Any help would be greatly appreciated. Or another solution for selecting the text in Excel. Note that VBA and Macros are also not working for the specific task I am attempting.

Thanks.

UPDATE using the advice from Sesame, I was able to get the text deleted. Now I want to select cells using shift+arrow or some work-around. Here is an updated code snippet; note the line that does not work:

        Robot r = new Robot();
        Sequence(r,n,KeyEvent.VK_ALT,KeyEvent.VK_TAB); //switch windows to Excel
        Thread.sleep(200);
        Press(r,n,KeyEvent.VK_SPACE); //replace text with space
        Select(r,n,KeyEvent.VK_CONTROL,KeyEvent.VK_Z); //UNDO to select text in cell
        Select(r,n,KeyEvent.VK_CONTROL,KeyEvent.VK_X); //CUT
        Sequence(r,n,KeyEvent.VK_CONTROL,KeyEvent.VK_SHIFT, KeyEvent.VK_ENTER); //Delete the array
        Select(r,n,KeyEvent.VK_SHIFT,KeyEvent.VK_RIGHT,KeyEvent.VK_RIGHT,KeyEvent.VK_RIGHT); //Select cells DOES NOT WORK
        Press(r,n,KeyEvent.VK_F2); //Open cell combination for typing array
        Sequence(r,n,KeyEvent.VK_CONTROL,KeyEvent.VK_SHIFT, KeyEvent.VK_ENTER); //Input the array
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Navigating to a cell and pressing space then ctrl + z (undo) selects the text in that cell. Basically it clears the square and when you undo the clear, the text is selected. It's ugly, but if that's all you need I suppose it could work.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...