[英]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. 我正在尝试使用java.awt.robot为我自动修复一些Excel数组。 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.
似乎shift + home和Shift +箭头键不适用于选择文本。 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.
请注意,如果在开始时注释掉alt + tab(第12行)并从Eclipse(或另一个IDE)运行,尽管它会移动光标,但它也不会选择Eclipse中的文本。 CTRL-A
does work , but won't select the text in Excel, so it'a a no-go.
CTRL-A 确实可以 ,但是不会在Excel中选择文本,因此这是不行的。
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. 我在这里找到有关此主题的一个参考https://community.oracle.com/thread/1289981?start=0&tstart=0 ,但是他们没有找到解决方案。
Any help would be greatly appreciated. 任何帮助将不胜感激。 Or another solution for selecting the text in Excel.
或用于在Excel中选择文本的另一种解决方案。 Note that VBA and Macros are also not working for the specific task I am attempting.
请注意,VBA和宏也不适用于我尝试的特定任务。
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.
现在,我想使用shift + arrow或一些替代方法来选择单元格。 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
Navigating to a cell and pressing space then ctrl + z (undo) selects the text in that cell. 导航到一个单元格并按空格,然后按ctrl + z (撤消)可以选择该单元格中的文本。 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.
这很丑陋,但如果您只需要这些,我想它就可以工作。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.