Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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!

like image 860
Kahtaf Alam Avatar asked Jan 27 '13 16:01

Kahtaf Alam


People also ask

What is KeyEvent in Robot class?

KeyEvent indicates an event that occurs on pressing, releasing, or typing a key on the component object like a text field. This KeyEvent class has various constant fields like VK_0, VK_1 till VK_9 of type integer.

What is VK in Robot class?

VK_META and META_MASK are defined in KeyEvent and InputEvent classes. They both define the META key as a standalone key pressed and as a modifier used pressing another key respectively. The META key is a key used in old keyboards and now can be emulated using the Windows Key.


2 Answers

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

like image 197
Konrad Reiche Avatar answered Sep 18 '22 20:09

Konrad Reiche


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.

like image 24
Kahtaf Alam Avatar answered Sep 19 '22 20:09

Kahtaf Alam