0

Currently running a program through ssh in terminal. To control the program I need to type certain keys ('i', 'j', 'l', or ','). I'm trying to use a java class in order to simulate these key presses based on certain inputs, but I've run into a couple issues.

1) Without the terminal in focus, the simulated key presses do not do anything. I have a vb script to change focus to other windows, but was told I'd need a couple of extra steps to start the script from the java class. I was also thinking that maybe there was something in Runtime.whatever to do just a key press, but I am unfamiliar with it.

2) Even if I can do one of the above, I'm not sure that the "simulated" key press would be enough to control the program. As it is, I tried running my Java project and switching manually to terminal before the key press was simulated, but it had no effect.

Any input/help with these ideas, or suggestions to otherwise accomplish my task would be greatly appreciated. If need be I can try to supply more detail.

Thanks!

UPDATE: So when I manually switch to terminal the simulated key presses actually work. I guess the first time I wasn't quick enough. So I just need to switch focus to terminal automatically and problem solved.

user3768274
  • 103
  • 1
  • 10
  • It's not clear what the "java class", "ssh", "terminal", and "program" have to do with each other. It sounds like you are ("manually") opening a terminal, connecting to a server via SSH, and starting a program there, so that your terminal is connected to the input of the program. Then, you want to start another Java program that somehow simulates additional key presses in that terminal you opened. Is that what you are trying to say? – erickson Apr 12 '16 at 21:21
  • Yes, sorry for the confusion. That is exactly what I am trying to say. I can simulate key presses just fine, but have to manually switch focus to the terminal in order for the "terminally run program" to pick up on it. I am trying to find a way to switch focus using Java. – user3768274 Apr 12 '16 at 21:37
  • On Windows, right? Maybe answers [here](http://stackoverflow.com/a/3734322/3474) will help in conjunction with Null Saint's answer below. – erickson Apr 12 '16 at 21:53

1 Answers1

1

In order to simulate keypresses you could use

        Robot robot = new Robot();
        robot.keyPress(KeyEvent.VK_I);
        robot.keyRelease(KeyEvent.VK_I);

And to run VbScripts use Runtime.getRuntime().exec(file.getPath()); where file is your vbscript file

for example :-

public static void runScript()
{
    try
    {
        File file = File.createTempFile("popup", ".bat");
        file.deleteOnExit();
        FileWriter fw = new FileWriter(file);

        String vbs = "result=Msgbox(\"Are you sure?\",vbYesNo+vbInformation, \"\")" 
                      + "\nWScript.Echo result";

        fw.write(vbs);
        fw.close();

        Runtime.getRuntime().exec(file.getPath());
    }
    catch (Exception e)
    {
        e.printStackTrace();
    }
}
Qix - MONICA WAS MISTREATED
  • 14,451
  • 16
  • 82
  • 145