0

I don't want user to kill the application with ctrl + c. I want them to kill the application with a word input from the console, like quit. So I add the following piece of code in. But I found if I ctrl + c first, then I cannot input any word on the console anymore. It seems the Signal blocks or eats all the keyboard input. how can I avoid this?

Signal.handle(new Signal("INT"), new SignalHandler()
{
    @Override
    public void handle(Signal signal)
    {
        System.out.println("Got signal:" + signal);
    }
});

Scanner keyboard = new Scanner(System.in);
while (!isShutdown())
{
    System.out.println("Enter command :");
    while (keyboard.hasNextLine())
    {
        System.out.println("Enter command (quit to exit):");
        String input = keyboard.nextLine();

        if (input != null)
        {
            System.out.println("Your input is : " + input);
            if ("quit".equals(input))
            {
                System.out.println("Exit programm");
            }
        }
    }
}
System.out.println("close keyboarddddddddd");
keyboard.close();

EDIT:

I found it is the "keyboard.hasNextLine()" doesn't block the process. It causes a loop execution. Probably the ctrl+C is also caught by the scanner but scanner doesn't know how to handle it?

Developer Guy
  • 2,318
  • 6
  • 19
  • 37
peterboston
  • 877
  • 1
  • 12
  • 24

0 Answers0