Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java key listener in Commandline

Most demo showing keyevent in Swing, what is the equivalent in commandline?

like image 629
JohnMax Avatar asked Oct 23 '10 18:10

JohnMax


People also ask

How do I use KeyListener?

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.

What is KeyListener in Java?

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).


1 Answers

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);
      }
    });
like image 135
javauser71 Avatar answered Sep 19 '22 06:09

javauser71