Invoking menu actions using java.awt.Robot

I am trying to write a simple plugin that invokes different actions using shortcuts. But for some reason its not working even if I have an active editor.

For example here is a simple snippet that invokes COMMAND + A (select all text).:

ApplicationManager.getApplication().invokeLater(new Runnable() {
                        @Override
                        public void run() {
                            try {
                                Robot robot = new Robot();
                                robot.keyPress(KeyEvent.META_MASK);
                                robot.keyPress(KeyEvent.VK_A);
                                robot.keyRelease(KeyEvent.VK_A);
                                robot.keyRelease(KeyEvent.META_MASK);
                            } catch (AWTException e) {
                                e.printStackTrace();
                            }
                        }
                    });

However, even COMMAND + Q is not recognized. I started this route because it seems easier to invoke actions without having to construct them by hand but I am not getting anywhere. Any help/suggestions most welcome!

Thanks

1 comment
Comment actions Permalink

It's better to work with action instances directly - you can get registered actions from ActionManager instance. If you need to work with shortcuts, check KeymapManager and Keymap classes, they provide mappings between action ids and keyboard/mouse shortcuts.

0

Please sign in to leave a comment.