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

[1046 byte] By [Funky_1321a] at [2007-11-27 7:31:51]
# 1
JTextComponent has a getSelectedText() method, and there are ways to get what component has focus.To copy it to another application, there is a java.awt.datatransfer.Clipboard class in 1.5 and up, I haven't played with it but it could be what you are looking for.
robtafta at 2007-7-12 19:12:05 > top of Java-index,Desktop,Core GUI APIs...
# 2

Hello!

Yeah I'm yusing the Clipboard class to get data from the text field because I first copy the text. But the problem is that the text field is a part of another windows application. So I'm trying to select and copy some text from an application and get data from Clipboard and bring it into my Java app. But the text in the foreign application's text field isn't selected even I simulate this combtination of key strokes [with use of Robot.keyPress(KeyEvent event)]:

CTRL(holding down)+SHIFT(holding down)+RIGHT AROW+RIGHT AROW+...+RIGHT AROW+release ctrl and shift+CTRL+C

Funky_1321a at 2007-7-12 19:12:05 > top of Java-index,Desktop,Core GUI APIs...
# 3
I believe that simulating Keystorkes only simulates it at the JRE level, and not at the OS level. You might need some JNI to do what you are trying to do.
robtafta at 2007-7-12 19:12:05 > top of Java-index,Desktop,Core GUI APIs...
# 4
> I believe that simulating Keystorkes only simulates it at the JRE level, > and not at the OS level. You might need some JNI to do what you are > trying to do.no. i tried to write something on notepad.exe and it works well.
j_shadinataa at 2007-7-12 19:12:05 > top of Java-index,Desktop,Core GUI APIs...
# 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

Funky_1321a at 2007-7-12 19:12:05 > top of Java-index,Desktop,Core GUI APIs...