0

I am developing a virtual keyboard application. Everything works fine except that what ever i write is written into the present application only not the one that is running behind.

I fell this is because ROBOT class in java only works for the application context that creates it.

Is there a work around for this?

Here is the code, i am trying to write ls in the terminal that is behind my java application:

        Robot robot = new Robot();

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

        robot.keyPress('l');
        robot.keyRelease('l');
        robot.keyPress('s');
        robot.keyRelease('s');

        robot.keyPress(KeyEvent.VK_ENTER);
        robot.keyRelease(KeyEvent.VK_ENTER);
luator
  • 4,769
  • 3
  • 30
  • 51
Revanth Kumar
  • 809
  • 12
  • 18

1 Answers1

2

judging from this example, the Robot class is able to send keystrokes to external applications - but they will arrive at the topmost window so you basically need to focus the window to which you want to have your keystrokes sent, there seems to be a variety of solutions for this, im guessing this one is the easiest :

https://stackoverflow.com/a/4782350/351861:

Alternatively, you can write a VBScript to activate another application. For example:

Set WshShell = WScript.CreateObject("WScript.Shell") 
WshShell.AppActivate("Firefox")

Then use Runtime.exec from your Java app to execute the script.

Community
  • 1
  • 1
specializt
  • 1,913
  • 15
  • 26
  • The example you mentioned works fine when i know what application will be next.But in my case i want to use this application as a replacement to normal physical keyboard.So i should be able navigate and type just like a normal keyboard. – Revanth Kumar May 22 '15 at 09:31
  • 1
    in that case you wont need to anything and this question is void since a virtual keyboard does exactly that -- it sends keystrokes to the topmost application and nothing else. Btw : "what application will be next" doesnt make any sense. There is no "next" in operating systems of any kind, only Z-indexes of graphical windows, the one with the highest Z-value receives broadcasted events. Thats how modern operating systems work. – specializt May 22 '15 at 10:07