break a loop if Esc was pressed

You’re currently making a command-line application which reads stuff from standard input and prints stuff to standard output. How buttons presses are handled depends entirely on the terminal in which your are running your program, and most terminals won’t send anything to your application’s stdin when escape is pressed.

If you want to catch key events, you’ll have to make a GUI application using AWT or Swing. If all you want is to terminate your program while it’s running, try pressing Ctrl+C (this works in most terminals).

Leave a Comment