I have a process that I started a (non-Java) CLI process with Java and I want to programatically send Ctrl+I key combination to it.
Is it possible?
I have a process that I started a (non-Java) CLI process with Java and I want to programatically send Ctrl+I key combination to it.
Is it possible?
If the external process is looking for a Ctrl-I character (which is the same as Tab, by the way) on its standard input, GreenGiant's solution would work. Since that didn't work, the process is apparently looking for key presses on its console (tty), something to which other programs simply do not have access. You would need to write native code to create a virtual tty and have the process use that. JNI is how you allow Java to invoke native code as part of the Java program.
You are unable to send KeyListener Events in a console window without the use of GUI.
See the following for more details:
Key Bindings in Command Prompt with Java?
and
If you started the other program using ProcessBuilder
, then you might be able to send the control sequence by writing the appropriate bytes to the process's input stream.
I'm not sure what the actual byte sequence is that you need to send, but based on what I'm finding, it seems like Ctrl+A is 1, Ctrl+B is 2, etc.
ProcessBuilder pb = new ProcessBuilder( "..." );
Process process = pb.start();
OutputStream stream = process.getOutputStream();
stream.write( 'I' - 'A' + 1 ); // send Ctrl+I
stream.flush();