Using the Keyboard "ContextMenu" button in Java

1.7k Views Asked by At

In the application I'm working on I'd like to listen for when the keyboard's context menu (right click) button is pressed.

Just to be clear, I'm talking about the button between Alt Gr and Ctrl on the right of the spacebar. I realise it is not on all keyboards (older, mac's etc), but I know that all of the keyboards which will be using this application will have the button.

I'd like to know if there is a simple KeyEvent or any other method for knowing when it has been pressed.

Thanks,

Dave

2

There are 2 best solutions below

0
On BEST ANSWER

You can check the key code when a key is pressed. The key code of the context menu key is 525.

You can check this for yourself:

public void keyPressed(KeyEvent e) {
    System.out.println(e.getKeyCode());
}
0
On

To echo @Thijs Wouters' answer, (a) this is always a great way to figure out what key codes are associated with what keys in Java, and (b), this key code for the context menu, 525, is 20D in hexadecimal, and is defined in Java (since 1.5) as

KeyEvent.VK_CONTEXT_MENU

for ease of code reading.