0

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?

Ivan Mushketyk
  • 6,911
  • 6
  • 40
  • 61
  • yes. it is possible. java key bindings should do the trick. – andrewdleach Feb 26 '16 at 22:37
  • @andrewdleach Maybe I am missing something, but I don't see how this will help me to programmaticaly send a Ctrl+I to a remote CLI process. – Ivan Mushketyk Feb 26 '16 at 22:40
  • Sorry, what does ctrl+I do in the console? Does it map to an interrupt? – ChiefTwoPencils Feb 26 '16 at 22:40
  • @ChiefTwoPencils I don't think so. It's just a control symbol. – Ivan Mushketyk Feb 26 '16 at 22:46
  • No. It cannot be done with Java, unless you're willing to make use of JNI. – VGR Feb 27 '16 at 00:16
  • @VGR Could you please provide more details? – Ivan Mushketyk Feb 27 '16 at 12:27
  • 1
    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. – VGR Feb 27 '16 at 13:50
  • @VGR Thank you for a good comment, this seems like a good approach. Feel free to convert it into an answer. I'll accept it. – Ivan Mushketyk Feb 27 '16 at 16:04
  • @OP: Are you using some OS (such as MS Windows, GNU/Linux, MacOS X, etc)? Would you mind naming it? – Zsigmond Lőrinczy Feb 28 '16 at 18:56
  • I am using Linux. I don't care about other OSs in this case – Ivan Mushketyk Feb 28 '16 at 19:14

3 Answers3

2

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.

VGR
  • 33,718
  • 4
  • 37
  • 50
0

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

Java key listener in Commandline

Community
  • 1
  • 1
Tom C
  • 724
  • 2
  • 10
  • 24
  • Sorry, but this is not what I was asking about. I need to programatically send Ctrl+I to a different CLI process from Java. – Ivan Mushketyk Feb 26 '16 at 22:42
0

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();
GreenGiant
  • 4,226
  • 1
  • 40
  • 69