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

倾然丶 夕夏残阳落幕 提交于 2021-01-29 15:53:29

问题


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

回答1:


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.



来源:https://stackoverflow.com/questions/24023187/how-can-i-select-text-in-another-application-with-java-awt-robot-example-shift

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!