Using Java to set the focus to a non Java application in Windows
I would like to write an application who creates input for a non-Java applica开发者_StackOverflowtion 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.
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.
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)
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);
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);
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();
}
}
}
精彩评论