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