'Ctrl-C for exit the program isn't working without enter

I was using Runtime -> addShutDownHook for closing application. It works with an enter, but only typing ctrl+c doesn't do it's job. Is it because I opened scanner at the beginning of the class? What would be better solution for doing ctrl + c for java?

while (flag){
    try{
        a = scanner.next();
    } catch (IllegalStateException e){
        return;
    } catch (Exception e){
        return;
    }

    if(option.length() == 1){
        flag = true
    } else {
                    
    }
}


Solution 1:[1]

Solved, try to use Scanner = new Scanner every roop than using on constructor.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 kzrt0123