Alt+Tab using Java Robot
Asked Answered
I

2

9

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!

Idaliaidalina answered 27/1, 2013 at 16:37 Comment(6)
Your code works fine for me using Windows 7 and JDK 7.Actinochemistry
Then this seems to be a Windows 8 thing, since with Windows 8 and JDK 7 I have the same problem.Bremser
What if you put some delays between other key presses and releases?Actinochemistry
Thanks for your responses. I have tried putting delays between each keyPress and keyRelease with no success.Idaliaidalina
1) For better help sooner, post an SSCCE. 2) Read tags before you go slapping them on posts. This has nothing to do with robot.Desensitize
ALT+TAB worked fine for me using Windows 10 Creators Edition.Transfer
I
2

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.

Idaliaidalina answered 4/2, 2013 at 15:27 Comment(1)
You can also customize a hotkey to the shortcut and then press it using the robot.Transfer
B
6

There is a Windows Dev Center thread where this very problem is discussed. Apparently the rules have changed in Windows 8.

Simulation of keyboard input, which can trigger responses in the Shell, are not guaranteed to work anymore, unless the application is an assistive technology application which has UiAccess privileges.

“An accessibility application can use SendInput to inject keystrokes corresponding to application launch shortcut keys that are handled by the shell. This functionality is not guaranteed to work for other types of applications.” — Send Input Function (Windows)

The following requirements have to be met:

  • be signed
  • be installed under %ProgramFiles% or %SystemRoot%\system32
  • specify uiAccess='true' in the manifest
  • run under SYSTEM or the currently logged-on user

Google Groups

Bremser answered 27/1, 2013 at 17:1 Comment(2)
If this is a limitation of Windows 8, is there any other way I can achieve this using Java?Idaliaidalina
@user2015783 I have updated my post with the requirements which have to be met for your program. These are a lot of requirements. I would definetely say this is not solvable from Java alone. Especially, I am not sure how to realize the manifest and signed application requirements.Bremser
I
2

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.

Idaliaidalina answered 4/2, 2013 at 15:27 Comment(1)
You can also customize a hotkey to the shortcut and then press it using the robot.Transfer

© 2022 - 2024 — McMap. All rights reserved.