0

It is pretty easy to read input from console after it was submitted by hitting Enter key. Such ways as System.console(), System.in, Scanner and more.

String out = System.console().readLine();

I wonder if there is a way to read unsubmitted characters from console (specially from CMD)

UPDATE

Best solution I found so far is to use some low-level library like JNativeHook.

ivanjermakov
  • 1,131
  • 13
  • 24
  • Not without native extensions, but libraries like jcurses should be able to. – kumesana Jun 27 '18 at 11:09
  • 1
    Are we talking about registering keystrokes? Cause if we are, you may wanna check out -> https://stackoverflow.com/questions/12177416/how-can-i-write-a-key-listener-to-track-all-keystrokes-in-java However if your Java app is not in focus as the main window on the pc screen don't think you can get any input registration due to process limitations... – bko Jun 27 '18 at 11:09
  • @bko this is not about a gui but about reading from console. While the console is focused. – f1sh Jun 27 '18 at 11:12
  • @bko Yeah something like keystrokes.. but in console, not windowed application. Thank you, I'll sort it out – ivanjermakov Jun 27 '18 at 11:12
  • @bko But what can I do within console window? Is it possible to use some native methods or so (Coz it's easily done in C or C++) – ivanjermakov Jun 27 '18 at 11:14
  • 1
    JNativeHook is about global system-wide events and not particularly tied to a console that would or would not be active at the moment. Though you can probably make things that look like they work, it's bound to come with its share of hiccups. I repeat my suggestion to use console-centered libraries that use native calls, such as jcurses. – kumesana Jun 27 '18 at 11:23
  • If your console is focused I'm pretty sure you can do it with a tiny bit of googling (for example: https://stackoverflow.com/questions/27381021/detect-a-key-press-in-console) on how to capture the keyboard input. Should be pretty straight forward. I did try to develop my own keylogger (for research purposes) in Java a while back and wasn't really satisfied how limited you are in terms of registering inputs (due to problems i mentioned above - focus) – bko Jun 27 '18 at 12:01
  • @bko What lib can you suggest for convenient usage? (all I need is just `keyPressed()` event) – ivanjermakov Jun 27 '18 at 12:03
  • Have a loot at this -> https://jline.github.io/ Because without some native fiddling you are unable to register changes in stream input without the press of the Enter key, which you don't want... – bko Jun 27 '18 at 12:05
  • After a bit of research it really does appear that you cannot do this without native c++ implementations like @kumesana mentioned above. Best bet it so play with JNativeHook. Other solutions I found work only after you press Enter – bko Jun 27 '18 at 12:17

0 Answers0