Wednesday 15 September 2021

Monday 13 September 2021

Simulate the pressing of the "Command" key on a Mac

Simulate the pressing of the "Command" key on a Mac

n the Robot class, the name for Command(⌘) is KeyEvent.VK_META. Your code should look like this:

Robot r = new Robot();
r.keyPress(KeyEvent.VK_META); //press command
r.keyPress(KeyEvent.VK_L); //press l
r.keyRelease(KeyEvent.VK_META); //release command
r.keyRelease(KeyEvent.VK_L); //release l

This presses the Command key, presses the L key, then releases them both. I have tested this, and it works as it should.

NOTE: Using Robot throws AWTException, so it requires you to either surround with try-catch block, or throw AWTException in your method header