Results 1 to 3 of 3
  1. #1
    Ryanbom is offline Member
    Join Date
    Sep 2008
    Posts
    1
    Rep Power
    0

    Default Help! How do I....

    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.

  2. #2
    serjant's Avatar
    serjant is offline Senior Member
    Join Date
    Jun 2008
    Location
    Ukraine,Zaporozhye
    Posts
    487
    Rep Power
    6

    Default

    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);

  3. #3
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default

    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.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •