Control + W to exit system

109 views Asked by At

I am working on a game and here I want the end-user to press Ctrl + W to exit the system.

Here is the code that I've used:

int key = e.getKeyCode();
if(key == KeyEvent.VK_CONTROL && key == KeyEvent.VK_W) System.exit(1);

... but it didn't seem to work

Can anyone tell me what's wrong with my code?

Thanks in advance!

2

There are 2 answers

5
Rich On

I would try something along the lines of:

int key = e.getKeyCode();
if(key == (KeyEvent.VK_CONTROL | KeyEvent.VK_W)) System.exit(1);

Though that's untested and is from memory.

1
chamzz.dot On
KeyStroke keyExit = KeyStroke.getKeyStroke(KeyEvent.VK_CONTROL, KeyEvent.VK_W); 
Action performExit = new AbstractAction("Exit") {  
    public void actionPerformed(ActionEvent e) {     
        //exit method
    }
};

try with this kind of example.