Win a copy of Cross-Platform Desktop Applications: Using Node, Electron, and NW.js this week in the JavaScript forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

How to make program quit if user presses ESC key?  RSS feed

 
Benjamin Mulder
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Darin Niard
Ranch Hand
Posts: 118
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That depends... is this in a GUI?
 
Benjamin Mulder
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks for your reply.

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


Thank you,
Ben
 
Joe Ess
Bartender
Posts: 9425
12
Linux Mac OS X Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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!
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!