Search code examples
javatabsawtawtrobotalt

Alt+Tab using Java Robot


I am trying to bring up the alt+tab menu with a Java Robot. When I call the alt_tab() method, I want to bring up the alt+tab menu and keep the menu up. I know this can be achieved using alt+ctrl+tab.

So far I have tried the code below, and also just alt+tab without the control key. I am not sure why it's not bringing up the menu. All it does is emulate pressing the alt key.

public void alt_tab() {
    Robot robot = new Robot();
    robot.keyPress(KeyEvent.VK_ALT);
    robot.keyPress(KeyEvent.VK_CONTROL);
    robot.keyPress(KeyEvent.VK_TAB);
    robot.delay(100);
    robot.keyRelease(KeyEvent.VK_TAB);
    robot.keyRelease(KeyEvent.VK_CONTROL);
    robot.keyRelease(KeyEvent.VK_ALT);
}

I am using Windows 8 Pro and JDK 7. Any help is appreciated!


Solution

  • I was able to find a workaround. I followed the instructions on this site to create a shortcut to the ALT+TAB menu, and use

    Runtime.getRuntime().exec("cmd \c start " + <path\to\shortcut\>);
    

    to launch the ALT+TAB menu without any special UIAccess privileges. Thanks to everyone for their responses.