This would be easy with robot.keyPress()
and robot.keyRelease()
but the problem is that I want the key to be pressed for the current focused application (not for the java console or IDE).
Key is just pressed. It is not pressed over any application. When key is pressed OS cares to send appropriate event to appropriate window. Typically this is the window that is in focus at the moment.
I believe that you are confused because you are running your code from IDE or from console, so the currently focused application is IDE or console. But try to do the following. Implment application that presses and then releases key A
but does it after some reasonable delay (e.g. 5 sec).
Now do the following. Start notepad, then start your application, then press alt-tab to focus the notepad and wait. When your application emulates key press, you should see the letter a
into your notepad.
Good luck.