21

What is the VK_[key] code for the command key on a mac, if one exists? I am trying to get a Robot (java Robot) to press the command key. I am using the command keyPress(), and I need to know the integer keycode for the command key on a mac.

4

3 回答 3

28

KeyEvent.VK_META, with key code 157, is Java's virtual key that maps to the the Mac command key.

于 2013-03-14T20:08:03.920 回答
1

KeyEvent.VK_META can be used as COMMAND button in Mac OS.

if its not working with you, that is because you need to add a delay

sample code for opening a new tab

Robot robot = new Robot();
robot.keyPress(KeyEvent.VK_META);
robot.delay(200);
robot.keyPress(KeyEvent.VK_T);
robot.keyRelease(KeyEvent.VK_META);
robot.keyRelease(KeyEvent.VK_T);
于 2021-09-16T09:28:24.493 回答
0

there is also isMetaDown() method, which in my case works if somebody wants to use the shortcuts for copy/paste text and so on.

Sample code:

public void keyPressed(KeyEvent e) {
    if (e.isMetaDown() && (e.getKeyCode() == KeyEvent.VK_V) && readonly.isSelected()){
        e.consume();
    }
}
于 2021-11-27T16:01:07.033 回答