Terminate a program with a specific key? For example, "press esc to exit or any other key to continue." Thank you in advance for your time.
Printable View
Terminate a program with a specific key? For example, "press esc to exit or any other key to continue." Thank you in advance for your time.
add keylistener to your JComponent and you should know the ID of KeyEvent ,if it equals to KeyEvent.VK_ESCAPE,then call the function System.exit(0);
If you're creating a Swing application (important information that you really should have in your initial post, you know), I'd recommend using key binding rather than keylisteners. The former works even if the JComponent to which it is bound doesn't have the focus. Also, it is a higher level abstraction than keylisteners and you should try to avoid lower level abstractions when they can be avoided. See the Sun key binding tutorial for more details on this.