Most demo showing keyevent in Swing, what is the equivalent in commandline?
A Simple KeyListener Java ClassCreate a new KeyListener object. Override the methods that correspond to the key events you want to monitor e.g keyPressed , keyReleased , keyTyped . Create a JTextField component. Use it's addKeyListener method to add to it the KeyListener you've created.
Interface KeyListenerThe listener interface for receiving keyboard events (keystrokes). The class that is interested in processing a keyboard event either implements this interface (and all the methods it contains) or extends the abstract KeyAdapter class (overriding only the methods of interest).
Following code will prevent the Ctrl+C combination to stop a CLI java program.
import sun.misc.Signal;
import sun.misc.SignalHandler;
Signal.handle(new Signal("INT"), new SignalHandler() {
// Signal handler method
public void handle(Signal signal) {
System.out.println("Got signal" + signal);
}
});
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With