I have a piece of code that is within an infinite loop. I would like to implement a method which checks whether I press a button or not as a loop breaker.
while(true)
{
...
if(this.escapeKeyIsPressed()) //Should return true if escape is pressed at that milisecond
{
...
break;
}
}
What is the easiest way to do this in java ? Which library should I look into ?
Thanks in advance for your time.
This page presents a method of setting the console into non-blocking mode in order to read a character, which you could use to break your loop. It also presents a few other methods for both Python and Java, but it has to be considered somewhat hacky and non-portable (wouldn’t work under Windows for example). I don’t think there is a ‘nice’ easy way to do it I’m afraid.