Robot will do fine for this problem, at least for part of this problem. Your main problem is this, I would imagine. Say you have three programs open: this Java program, Notepad, and a browser. At most, one of them will have the active focus. Or none of them could. So you have four possibilities for the key press:
- Focus is on Java program. Key press event goes to Java program.
- Focus is on Notepad program. Key press event goes to Notepad program.
- Focus is on browser program. Key press event goes to browser program.
- Focus is on none of the programs. Key press event goes to none of them.
You have a Java GUI with buttons, so in order for your actionPerformed to get called, the focus has to be on the Java program. You can easily use Robot to send a key stroke, but it'll go to the Java program, since that's what the focus is. Which you don't want. You want the key stroke to be sent to Notepad. So you need to somehow change that focus to Notepad. You can do that manually, by clicking Notepad after clicking the button on the Java program. So you could click the button on the Java program, then have a pause of three seconds or whatever, enough time to let you manually click Notepad active. Then the pause is over, so have Robot send that key press, which will end up on Notepad.
That's one way. or, presuming that Java knows where Notepad is on the screen, you can have Robot do the mouse press onto Notepad for you, then send the key press.
There's probably another way to set the focus that doesn't involve mouse clicking, or where you can actually specify the Notepad process number or something, but I don't know what it is. But changing focus from Java to Notepad and back is your main issue with this program, I would imagine.