Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Control a Windows apps using Java

Tags:

java

windows

I would like to know if there is any way I can control a Windows application using Java code. I have already googled it, and found that it can be done using JNI or a library called NewJawin.

I want to control Windows Media Player using Java code, e.g. play, pause, and change songs, but could find no relevant example to get me started so far. Do you guys have any suggestion?

like image 378
AntonioJunior Avatar asked Feb 27 '23 08:02

AntonioJunior


1 Answers

As no one has answered this question, I thought I would.

public void firePlay() {
    //CTRL + P
    //import java.awt.Robot
    //import java.awt.KeyEvent
    try {
        Robot robot = new Robot();
        robot.keyPress(KeyEvent.VK_CONTROL);
        robot.keyPress(KeyEvent.VK_P);
        robot.keyRelease(KeyEvent.VK_P);
        robot.keyRelease(KeyEvent.VK_CONTROL);
    } catch (AWTException ex) {
        Logger.getLogger(atest.class.getName()).log(Level.SEVERE, null, ex);
    }
}

This would play/pause the video. You can see other shortcuts here(http://windows.microsoft.com/en-AU/windows-vista/Windows-Media-Player-keyboard-shortcuts)

like image 117
Opal Avatar answered Mar 04 '23 10:03

Opal