1

I have an interactive command line program, that has tab completion feature (completes the command or list possible options) when tab key is pressed.

I'm writing another Java program, that will launch the command line program as a child process with ProcessBuilder.

I'm able to pass the input to the process using Process.getInputStream() and Process.getOutputStream().

Now, I want to trigger the auto completion in the child process. When I print the tax character '\t', it is treated as a regular tab character, instead of triggering the tab auto completion.

Is there a way to send key press or equivalent event from a Java process to a child process?

JackDaniels
  • 985
  • 9
  • 26
  • Hey Jack, did you gave a look to this https://stackoverflow.com/questions/3537706/how-to-unescape-a-java-string-literal-in-java – Diego López Sep 24 '17 at 23:23
  • That seems different. In this case, if I write character '\t' then the child process reads single tab character. When I send "\\t" (effectively two characters) the child process reads two characters. But what I want is, to send key press event – JackDaniels Sep 25 '17 at 00:05
  • Features like completion are often disabled when the program thinks it's being used noninteractively; for bash (4.1) in particular, readline is enabled only if the shell is "started without non-option arguments and without the -c option [and] standard input and error are both connected to terminals (as determined by isatty(3)), or ... started with the -i option". Java `ProcessBuilder` by default, and `Runtime.exec` always, uses pipes for the child's stdin/out/err, and pipes return false from isatty. For other shells or programs check their documentation. – dave_thompson_085 Sep 25 '17 at 02:35

1 Answers1

0

You could try using a Robot

Robot robot = new Robot();
robot.mouseMove(x, y); //Select the console window at x,y
robot.keyPress(KeyEvent.VK_TAB); //Send tab key  

Or also running OS specific commands, for example xdotool for Unix using X11

xdotool windowactivate id &&
xdotool key Tab &&
Diego López
  • 1,559
  • 1
  • 19
  • 27