开发者

Using the Keyboard "ContextMenu" button in Java

开发者 https://www.devze.com 2023-02-08 19:10 出处:网络
In the application I\'m working on I\'d like to listen for when the keyboard\'s context menu (right click) button is pressed.

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


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.


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

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号