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.
3 Answers
KeyEvent.VK_META, with key code 157, is Java's virtual key that maps to the the Mac command key.
3 Comments
lony
Is this still working for you? For me this is sadly not working.
FThompson
@lony Do you have simple example code I can run to test whether it's working or not? I could write my own test but if you have something that could save me the time that would be much appreciated.
lony
The problem for me was that I needed a delay in between this and the other key selects. Then it worked.
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);