Listen for key press in loop

Ok, I am trying to find a way to listen for a key press on every iteration through a loop. I do not want to use the Scanner, as this will stop the loop through every iteration and wait for input before continuing.

what i need to do is have something that will not stop the loop and wait for a key press, but can detect if a key is pressed at any time the loop is iterating.

Does that make sense, and is this possible?

Help would be greatly appreciated!

[478 byte] By [Arduciusa] at [2007-10-3 9:27:41]
# 1
Are you talking about on the console? What happens in the loop? Have you looked at a KeyListener?
zadoka at 2007-7-15 4:41:59 > top of Java-index,Java Essentials,New To Java...
# 2

Sorry I should have been more clear.

Im talking about the any keyboard key press.

The loop simply prints out a "z" then sleeps for half a second.

Just looked at KeyListener, is it only for graphical programs? or will it be fine for a command line based one?

Either I am implementing it wrong, or it cant be used in this situation.

Any other suggestions?

Arduciusa at 2007-7-15 4:41:59 > top of Java-index,Java Essentials,New To Java...
# 3

You could create a super simple GUI with just a text area that could look just like a console and then you could use a key listener.

Otherwise you are going to have to poll the console to see when something is typed then.I would suggest you research Threads in order to keep from having your reading stop the loop.

zadoka at 2007-7-15 4:41:59 > top of Java-index,Java Essentials,New To Java...