Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to implement alt+tab like feature using java?

Tags:

java

javafx

jna

Image shows what getDisplayMedia() does

I want to show user, all the current opened applications/windows (like that of alt+tab) using java. In javaScript we can do this by Media Devices interface getDisplayMedia(). I want to implement similar feature using java. Is there any way to do this using JNA, or something else.

like image 257
Nirman Avatar asked Jul 22 '19 10:07

Nirman


1 Answers

The method I'm sharing does the trick of pulling up the alt+tab menu using Java Robot and letting it close after a delay (It switches the window as well). Hope this helps!

public static void alt_tab() {
    Robot robot;

    try {
        robot = new Robot();
        robot.keyPress(KeyEvent.VK_ALT);
        robot.keyPress(KeyEvent.VK_TAB);
        robot.keyRelease(KeyEvent.VK_TAB);
        robot.delay(5000);
        robot.keyRelease(KeyEvent.VK_ALT);
    } catch (AWTException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

}

Cheers!

like image 72
JDevr Avatar answered Oct 01 '22 07:10

JDevr