0

Can anyone tell me what the Robot Keypress is for the "control" key on mac? I've tried VK_Control, VK_META, CTRL_MASK and CTRL_DOWN_MASK still nothing.

Please note: Its the only control key on mac next to the "fn" key lefthand side. Not option or command. Thanks

eltabo
  • 3,749
  • 1
  • 21
  • 33
Laser Hawk
  • 1,988
  • 2
  • 23
  • 29
  • I'm not sure why KeyEvent.VK_CONTROL doesn't work. Can you provide a runnable example of what you gave tried? – MadProgrammer Jan 15 '14 at 20:47
  • Something else i should mention is that I am trying to have the Android Emulator rotate `robot.keyPress(KeyEvent.VK_CONTROL); robot.keyPress(KeyEvent.VK_F12); robot.keyRelease(KeyEvent.VK_CONTROL); robot.keyRelease(KeyEvent.VK_F12);` – Laser Hawk Jan 15 '14 at 21:35
  • Try adding a [`autoDelay`](http://docs.oracle.com/javase/7/docs/api/java/awt/Robot.html#setAutoDelay(int)), I've found this helps in a lot of circumstances – MadProgrammer Jan 15 '14 at 22:10
  • YES! that was it thank you sir. Pausing the thread in between methods is the trick. Problem solved – Laser Hawk Jan 16 '14 at 17:49

2 Answers2

0

I think it's code 59 - see here.

Mark Setchell
  • 191,897
  • 31
  • 273
  • 432
  • Using __magic numbers__ is actually a bad idea. It may change any time, and cannot be easily read, refactored or searched. – Seagull Jan 15 '14 at 20:59
  • 59 didn't not work, but great reference of keycodes. I wish it was a newer mac keyboard representation. Thanks though, close. – Laser Hawk Jan 15 '14 at 21:25
0

It's bad to work with numeric constants. I can provide running example in Groovy, that will proof that Robot.keyPress(KeyEvent.VK_CONTROL) is working perfectly. May it be, that you forgot call releaseKey?

P.S. Tested on Macos Maveric with Java 1.6 with such snippet.(Groovy)

EDITED (I may suppose, you need to change Ctrl and F12 release order. F12 should be released, while Ctrl is still pressed, then modifiers will be setted correctly, and F12 will be treated as Ctrl+F12)

new SwingBuilder().frame(pack: true, show: true, defaultCloseOperation: JFrame.EXIT_ON_CLOSE) {
    textField(keyPressed: { KeyEvent e ->
        println("$e.keyCode, $e.modifiers") // prints 123 2 in response to Robot event.
        if (e.keyCode == KeyEvent.VK_SPACE)
        {
            new Robot().with {
                robot.keyPress(KeyEvent.VK_CONTROL);
                robot.keyPress(KeyEvent.VK_F12);
                robot.keyRelease(KeyEvent.VK_F12); // Release it first.
                robot.keyRelease(KeyEvent.VK_CONTROL);
}    }     }) }
Seagull
  • 13,484
  • 2
  • 33
  • 45
  • I am using release in the keyevent, but still not getting the results i need. `robot.keyPress(KeyEvent.VK_CONTROL); robot.keyPress(KeyEvent.VK_F12); robot.keyRelease(KeyEvent.VK_CONTROL); robot.keyRelease(KeyEvent.VK_F12);` – Laser Hawk Jan 15 '14 at 21:28
  • @jermobileqa Answer edited. I can't check, is Android emulator get correct keystroke, but it works in other apps(checked Ctrl+O, Ctrl+S) – Seagull Jan 15 '14 at 23:51