使用AWT Robot在Mac上切换应用程序有时只能工作 [英] Switching Apps on Mac with AWT Robot only sometimes works

查看:98
本文介绍了使用AWT Robot在Mac上切换应用程序有时只能工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用Robot来切换应用,然后输入一些文字。要做到这一点(在我的Mac上),我按下Meta,Tab,然后按顺序释放Tab,Meta:

I'm trying to use Robot in order to switch apps, and then enter some text. To do this (on my mac), I'm pressing Meta, Tab, and then releasing Tab, Meta in this order:

Robot robot = new Robot();
robot.keyPress(KeyEvent.VK_META);
robot.keyPress(KeyEvent.VK_TAB);

robot.keyRelease(KeyEvent.VK_TAB);
robot.keyRelease(KeyEvent.VK_META);

这是有效的,但只是偶尔(大约每5或6次按下)。我试过在按下和释放之间调用Thread.wait(),但这没有效果。也没有尝试使用META_DOWN_MASK屏蔽Tab。我也尝试使用JavaFX Robot(com.sun.glass.ui.Robot),但JavaFX版本根本不起作用。

This works, but only occasionally (about every 5 or six presses). I've tried calling Thread.wait() inbetween press and release, but this has no effect. Neither does trying to mask Tab with META_DOWN_MASK. I also tried using the JavaFX Robot (com.sun.glass.ui.Robot), but the JavaFX version doesn't work at all.

推荐答案

啊..似乎我需要指定事件之间的延迟。更新:

Ah.. Seems that I need to specify a delay between the events. Updated:

            Robot robot = new Robot();
            robot.keyPress(KeyEvent.VK_META);
            robot.delay(300);
            robot.keyPress(KeyEvent.VK_TAB);

            robot.keyRelease(KeyEvent.VK_TAB);
            robot.keyRelease(KeyEvent.VK_META);

这篇关于使用AWT Robot在Mac上切换应用程序有时只能工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆