1

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!

Star
  • 103
  • 9

2 Answers2

2
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.

chamzz.dot
  • 607
  • 2
  • 12
  • 24
  • The code isn't seeming to work... Where you commented `//exit method` I typed `System.exit(1);` – Star Jun 19 '18 at 12:24
0

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.

Rich
  • 15,602
  • 15
  • 79
  • 126