17

I would like to write an application who creates input for a non-Java application in Windows. With the Robot class it's easy to generate the input, but I need to set the focus to another application's text box and enter the text over there.

Don't worry I'm not trying to write something malicious, I just want to use Java to "extend" an old application written in Delphi.

2
  • 1
    An alternative option is that my Java application never gets the focus, so that the windows application has the focus and when you click a button on the Java application the focus is never lost in the original window. I have no idea if that's possible? Commented Jan 24, 2011 at 13:31
  • for those coming to this question via google, on OSX: stackoverflow.com/questions/14859733/… Commented Feb 13, 2013 at 18:32

5 Answers 5

11

CMDOW is a command line utility which allows you to perform various window actions such as activating/deactivating, listing, minimising/maximising etc.

Alternatively, you can write a VBScript to activate another application. For example:

Set WshShell = WScript.CreateObject("WScript.Shell") 
WshShell.AppActivate("Firefox")

Then use Runtime.exec from your Java app to execute the script.

This will help you activate another application.

However, it will be much more difficult if you want to focus on a textbox within the other application and write some text.

Sign up to request clarification or add additional context in comments.

2 Comments

just have to say that I like CMDOW but I noticed 2 problems.. very slow.. on my overloaded thermal throttled system - ordering a new fan now - so for most users don't know if it will be noticeable, however nircmd worked much faster.. so just letting people know nircmd is an option too - doesn't support wildcards but you can specify if substring match or from start or from end of title. Also I couldn't get it to activate multiple windows with the same substring, nircmd could do this.. so for these 2 reasons I chose nircmd instead..
@dogbane do you know something similar for linux os?
9

Configure a delay otherwise it won't work:

Robot r = new Robot();
r.keyPress(KeyEvent.VK_ALT);
r.keyPress(KeyEvent.VK_TAB);
r.delay(10); //set the delay
r.keyRelease(KeyEvent.VK_ALT);
r.keyRelease(KeyEvent.VK_TAB);

Comments

7

Detecting a special application and bringing that one to the front might require a native helper, but for the moment you could send ALT+TAB to activate the "next" application

This works:

public void switchFocus() {
  try {
    Robot r = new Robot();
    r.keyPress(KeyEvent.VK_ALT);
    r.keyPress(KeyEvent.VK_TAB);
    r.keyRelease(KeyEvent.VK_ALT);
    r.keyRelease(KeyEvent.VK_TAB);
  } catch(AWTException e) {
    // handle
  }
}

you just need to implement a convenience method to map chars (from a String) to key event values... (or find some existing solution)

Comments

2

On Mac, there is possible to do it with AppleScript. AppleScript is integrated to system, so it will be always functional. https://developer.apple.com/library/content/documentation/AppleScript/Conceptual/AppleScriptLangGuide/reference/ASLR_cmds.html

You need only detect you are on mac and has name of the application.

Runtime runtime = Runtime.getRuntime();
            String[] args = { "osascript", "-e", "tell app \"Chrome\" to activate" };
            Process process = runtime.exec(args);

Comments

0

You need to add enough delay for the application to fully initialize and gain focus.

Here's a basic working example... Andreas_D is correct that you need to emulate the system key to switch between programs... (Alt+Tab on Windows, Cmd+Tab on OS X)

    import java.awt.*;
    import static java.awt.event.KeyEvent.*;
    import java.io.IOException;

    public class RobotSample {

        //https://stackoverflow.com/questions/4782231
        private static Integer[] KEY_CODES = { VK_S, VK_T, VK_A, VK_C, VK_K, VK_O, VK_V, VK_E, VK_R, VK_F, VK_L,VK_O, VK_W, VK_DECIMAL, VK_C, VK_O, VK_M, VK_SLASH, VK_Q, VK_U, VK_E, VK_S, VK_T, VK_I, VK_O, VK_N, VK_S, VK_SLASH, VK_4, VK_7, VK_8, VK_2, VK_2, VK_3, VK_1, VK_ENTER };

        public static void main( String[] args ) throws IOException {

            try {
                Robot robot = new Robot();
                Runtime runtime = Runtime.getRuntime();

                runtime.exec( "C:\\Program Files (x86)\\Google\\Chrome\\Application\\chrome.exe" );

                robot.keyPress( VK_ALT );
                robot.keyPress( VK_TAB );
                robot.keyRelease( VK_ALT );
                robot.keyRelease( VK_TAB );

                //Chill a sec...
                robot.delay( 1000 );

                for(int i = 0; i <  KEY_CODES.length; ++i) {
                    robot.keyPress( KEY_CODES[i] );
                    robot.keyRelease( KEY_CODES[i] );
                    robot.delay( 80 );
                    }

            } catch( AWTException e ) {
                e.getMessage();
            }

        }

    }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.