Can I move another program window to focus?

I interact with a third-party application using its API and would like to move it to focus focus (so that it is on top of all other open windows) when the user performs a certain action. Although I can easily move the application up and down in the hierarchy, there is no way to interact with other windows. Is it possible to move another program window to the beginning with Java?

+5
source share
3 answers

You cannot do this in pure Java code, but you can use JNI. See In Java Swing, how do you get a link to a Win32 window handle (hwnd) for a window? how to get a window handle. Then you can do something like http://msdn.microsoft.com/en-us/library/ms633545 to move it to the front.

Caution: this is for windows only

+4
source

Java , (, OS X 10.4: OS X 10.4 Windows... "", , , , " ", OS X).

+1

. - ! - . , java.awt.Robot , .

java.awt.Robot, , Alt+Tab, . , , , :

  • , Alt+Tab , . - . , , Alt-Tab -count.
  • - .

, , , Alt-Tab, .

Java , . , Alt+Tab . , , , Alt+Tab , .

, robot.delay() robot.setAutoDelay(), . : , Linux, robot.setAutoDelay(), , , Alt-Tab - .

import java.awt.AWTException;
import java.awt.Robot;
import static java.awt.event.KeyEvent.VK_ALT;
import static java.awt.event.KeyEvent.VK_TAB;
import static java.lang.Integer.parseInt;

public class WindowSwitcher {
    public static void main(final String... args) throws AWTException {
        final int repeat = args.length != 0 ? parseInt(args[0]) : 1;
        final Robot robot = createRobot();
        robot.keyPress(VK_ALT);
        for (int i = 0; i < repeat; i++) {
            robot.keyPress(VK_TAB);
            robot.keyRelease(VK_TAB);
            robot.delay(500);
        }
        robot.keyRelease(VK_ALT);
    }
    public static Robot createRobot() throws AWTException {
        final Robot robot = new Robot();
        robot.setAutoWaitForIdle(true);
        robot.setAutoDelay(10);
        return robot;
    }
}
0

All Articles