views:

196

answers:

2

I am writing an application in Java that places an icon in the system tray (via SWT). When this icon is clicked, I wish to have it automatically type some keys (via the Robot class) into whatever text field is in focus at the time of clicking (could be in any window). Unfortunately, clicking the system tray icon steals the focus away from the previously focused window, thereby stealing the key strokes.

Is there a way to cause the text to be typed into the previously focused window?

A: 

Probably not, at least not easily. This issue been discussed before.

jdigital
A: 

Although it cannot be considered an official solution, I have been finding some success by issuing ALT+TAB key strokes immediately before issuing the textual key strokes.

Robot robot = new Robot();

robot.keyPress(KeyEvent.VK_ALT);
robot.keyPress(KeyEvent.VK_TAB);
robot.keyRelease(KeyEvent.VK_ALT);
robot.keyRelease(KeyEvent.VK_TAB);

// ... The keyPress/keyRelease pairs for the actual characters now begin

Like I said, this is far from being an official solution, considering the ALT+TAB combination cannot be relied upon to be a universal focus transition command. However, it seems to be fitting the bill for my particular situation.

Adam Paynter