Reputation: 10102
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 :)
Upvotes: 1
Views: 3832
Reputation: 115328
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
Upvotes: 1
Reputation: 8467
Not sure if there is a way to attach eventLinsteners to a console app,
Console app? You could just read System.in
and do whatever you want to do read character/string
Scanner sc = new Scanner(System.in);
String inputString = sc.nextLine(); // you could swith it with nextInt(), nextFloat() etc based on yoir need
If(inputString.equals("whatever")
{
//do whatever , look into Runtime.exec to execute programs
}
Upvotes: 0