使用 Java Robot 的 Alt+Tab

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14549526/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 16:37:34  来源:igfitidea点击:

Alt+Tab using Java Robot

javatabsawtawtrobotalt

提问by Kahtaf Alam

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

我正在尝试使用 Java 机器人调出alt+tab菜单。当我调用 alt_tab() 方法时,我想调出alt+tab菜单并保持菜单。我知道这是可以使用可实现alt+ ctrl+tab

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

到目前为止,我已经尝试了下面的代码,也只是alt+tab没有控制键。我不知道为什么它没有调出菜单。它所做的只是模拟按下 alt 键。

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!

我使用的是 Windows 8 Pro 和 JDK 7。感谢您的帮助!

采纳答案by Kahtaf Alam

I was able to find a workaround. I followed the instructions on thissite to create a shortcut to the ALT+TABmenu, and use

我找到了解决方法。我按照站点上的说明创建了ALT+TAB菜单的快捷方式,并使用

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

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

在没有任何特殊 UIAccess 权限的情况下启动ALT+TAB菜单。感谢大家的回应。

回答by Konrad Reiche

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

有一个Windows Dev Center 线程讨论了这个问题。显然,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 UiAccessprivileges.

可以在 Shell 中触发响应的键盘输入模拟不能保证再工作,除非该应用程序是具有UiAccess特权的辅助技术应用程序。

“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)

“可访问性应用程序可以使用 SendInput 注入与外壳处理的应用程序启动快捷键对应的击键。不保证此功能适用于其他类型的应用程序。” —发送输入函数 (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 SYSTEMor the currently logged-on user

Google Groups

  • 签署
  • 安装在%ProgramFiles%%SystemRoot%\system32
  • uiAccess='true'在清单中指定
  • SYSTEM或当前登录的用户下运行

谷歌群组