javajline

Detect Ctrl and Alt keys at a Java console


I'm currently using JLine (http://jline.sourceforge.net/) to read characters from the console in Java one at a time.

Terminal terminal = TerminalFactory.getFlavor(TerminalFactory.Flavor.WINDOWS);
terminal.init();
terminal.setEchoEnabled(false);

int i;
InputStream in = terminal.wrapInIfNeeded(System.in);

i = in.read();

How can I detect if the Ctrl or Alt keys are pressed when I called in.read();? I can't figure out from either the javadocs or any other source how to read if the Ctrl or Alt key is pressed.


Solution

  • You need to get keyboard scan codes which can be done using a native library. See: How can I get the keyboard scan code in java? Specifically: https://github.com/kwhat/jnativehook