Jump to content

capture key

- - - - -

  • Please log in to reply
1 reply to this topic

#1
alix

alix

    Newbie

  • Members
  • PipPip
  • 28 posts
i'm interested in capturing the escape key
whenever i press it i want system.exit(1);
any help on that?

#2
wim DC

wim DC

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 2,084 posts
  • Programming Language:Java, JavaScript, PL/SQL
  • Learning:Java
addKeyListener(new KeyListener() {

    public void keyTyped(KeyEvent e) {}


    public void keyPressed(KeyEvent e) {

        if(e.getKeyCode() == KeyEvent.VK_ESCAPE){

            frame.dispose();

        }

    }


    public void keyReleased(KeyEvent e) {}

});

You do the addKeyListener on the object where you want to catch it on. Could be a JFrame, JPanel, JTextField, any gui component

JFrame frame;

...

frame.addKeyListener(...);

extra info: How to Write a Key Listener (The Java™ Tutorials > Creating a GUI With JFC/Swing > Writing Event Listeners)




1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users