我正在尝试将焦点从一个 IntelliJ 实例转移到另一个实例。尽管窗口显示在所有内容的顶部并且光标开始闪烁,但焦点实际上位于上一个窗口中,并且图标在任务栏中闪烁。
在一个进程的帧之间切换没问题,但切换到另一个进程就会出现问题。
Windows 7 x64、jdk1.7.0_51
JFrame frame = WindowManager.getInstance().getFrame(project);
//the only reliable way I found to bring it to the top
boolean aot = frame.isAlwaysOnTop();
frame.setAlwaysOnTop(true);
frame.setAlwaysOnTop(aot);
int frameState = frame.getExtendedState();
if ((frameState & Frame.ICONIFIED) == Frame.ICONIFIED) {
// restore the frame if it is minimized
frame.setExtendedState(frameState ^ Frame.ICONIFIED);
}
frame.toFront();
frame.requestFocus();
//frame.requestFocusInWindow(); same behaviour as requestFocus
我还尝试了机器人,并按照其他问题中的建议创建了新的临时框架,但没有成功。
最佳答案
似乎我之前错误地使用了 Robot hack,因为现在它似乎工作正常:
try {
//remember the last location of mouse
final Point oldMouseLocation = MouseInfo.getPointerInfo().getLocation();
//simulate a mouse click on title bar of window
Robot robot = new Robot();
robot.mouseMove(frame.getX(), frame.getY());
robot.mousePress(InputEvent.BUTTON1_DOWN_MASK);
robot.mouseRelease(InputEvent.BUTTON1_DOWN_MASK);
//move mouse to old location
robot.mouseMove((int) oldMouseLocation.getX(), (int) oldMouseLocation.getY());
} catch (Exception ex) {
//just ignore exception, or you can handle it as you want
} finally {
frame.setAlwaysOnTop(false);
}
关于java - 如何窃取 Windows 7 的焦点,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22055708/