Forums Register Login

How to make program quit if user presses ESC key?

+Pie Number of slices to send: Send
on a while loop...

Instead of using the regular sentinel value "press -1 to quit" I want to tell the user "press esc to quit".

How do I do that programmatically? How do I tell Java that "esc" key means that the program should automatically quit?

Thanks,
Ben
+Pie Number of slices to send: Send
That depends... is this in a GUI?
+Pie Number of slices to send: Send
thanks for your reply.

I want to learn it both ways: GUI and shell prompt


Thank you,
Ben
+Pie Number of slices to send: Send
Not easily done. You can't read a single key press off of the console in Java without resorting to native code. You can add a key listener to a GUI component but you will only get key presses if the component has focus. If you make a frame a key press listener and a button on the frame has the focus the frame will not get the event. Tell your users it is the 21st century, pick up a mouse!
Politics n. Poly "many" + ticks "blood sucking insects". Tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com


reply
reply
This thread has been viewed 4351 times.
Similar Threads
Press any key to continue...
Using Java How will u check key typed from the Keyboard is ESC key or not
Executing VLC in Java swing
Good text editor in Linux
Pressing ENTER when Button is selected doesnt fire buttonActionPerformed...
More...

All times above are in ranch (not your local) time.
The current ranch time is
Mar 28, 2024 10:54:44.