Is it possible to bind an action listener for keyboard events? E.g., I would like to write a program which computes something in the background, and when a key is pressed on the keyboard, the program executes a callback (a block of code).
Needless to say that the program is a command line program without any fancy GUI.
Any short snippet will be great 🙂
As far as I understand you want to enable non blocking IO from console application. This cannot be done with JDK only, but fortunately there are several 3rd party libraries that enable this functionality.
Take a look on the following discussions:
What's a good Java, curses-like, library for terminal applications?
How to determine if anything has been entered into the console window?
how can I detect arrow keys in java console not in GUI?
Listening to system mouse clicks from Java