marking text in other win app using Robot
Hello!
I'm trying to copy some text from another application.
First I move the mouse to the proper position, click into the text box and than press down the shift key and control key (and hold them down both), then is pressed the right arow key 7 times and ctrl and shift key are released. Than comes key commands for copying the text but my code doesn't marks the text!
Here's the code:
r.mouseMove(864, 198);
r.mousePress(InputEvent.BUTTON1_MASK);
r.mouseRelease(InputEvent.BUTTON1_MASK);
r.keyPress(KeyEvent.VK_CONTROL);
r.keyPress(KeyEvent.VK_SHIFT);
for (int i = 0; i < 7; i++){
r.keyPress(KeyEvent.VK_RIGHT);
r.keyRelease(KeyEvent.VK_RIGHT);
}
r.keyRelease(KeyEvent.VK_SHIFT);
r.keyRelease(KeyEvent.VK_CONTROL);
// r is instance of a Robot class
Thanks for future help!
Tilen
# 5
I don't get it. I also tried with notepad.exe and it doesn't work. Here's the code sample:
import java.awt.Robot;
import java.util.Timer;
import java.util.TimerTask;
import java.awt.event.KeyEvent;
import java.awt.AWTException;
public class MarkingText {
/** Ustvari nov primerek razreda MarkingText */
public MarkingText() {
TimerTask opravilo = new TimerTask() {
public void run() {
try {
Robot r = new Robot();
r.keyPress(KeyEvent.VK_CONTROL);
r.keyPress(KeyEvent.VK_SHIFT);
r.keyPress(KeyEvent.VK_RIGHT);
r.keyRelease(KeyEvent.VK_RIGHT);
r.keyPress(KeyEvent.VK_RIGHT);
r.keyRelease(KeyEvent.VK_RIGHT);
r.keyPress(KeyEvent.VK_RIGHT);
r.keyRelease(KeyEvent.VK_RIGHT);
r.keyRelease(KeyEvent.VK_SHIFT);
r.keyRelease(KeyEvent.VK_CONTROL);
} catch (AWTException e) { e.printStackTrace(); }
}
};
Timer casovnik = new Timer();
casovnik.schedule(opravilo, (long)3000);
}
public static void main(String[] args) {
new MarkingText();
}
}
So after executing the program, I place the cursor in notepad (with some text entered) at some position and wait 3 secs, than 3 words should be automaticly selected. Do anyone has a clue why isn't it working?
Thanks,
Tilen