The robot will send keystrokes depending on which application window is on top. To send keystrokes to a specific target, you first want to set the target as the platform foreground window. This may require native code such as JNI or (what I use) JNA. If you want to send keystrokes to the background window, I believe that you cannot use Robot, that you have to write your own code. Of course, all source code solutions will be platform dependent.
You are asking:
Could you give me more details on how to do this using JNA sorry I am writing for windows
On Windows, you must interact with the user32.dll library. Something like this worked for me:
User32.java
import com.sun.jna.Native; import com.sun.jna.Pointer; import com.sun.jna.platform.win32.BaseTSD.LONG_PTR; import com.sun.jna.platform.win32.WinDef.HWND; import com.sun.jna.platform.win32.WinDef.RECT; import com.sun.jna.win32.StdCallLibrary; public interface User32 extends StdCallLibrary { User32 INSTANCE = (User32) Native.loadLibrary("user32", User32.class); interface WNDENUMPROC extends StdCallCallback { boolean callback(Pointer hWnd, Pointer arg); } public static final int GW_OWNER = 4;
JnaUtil.java
import java.awt.AWTException; import java.awt.Rectangle; import java.awt.Robot; import java.awt.image.BufferedImage; import java.util.ArrayList; import java.util.List; import javax.swing.ImageIcon; import javax.swing.JLabel; import javax.swing.JOptionPane; import javax.swing.SwingUtilities; import com.sun.jna.Native; import com.sun.jna.Pointer; import com.sun.jna.platform.win32.BaseTSD.LONG_PTR; import com.sun.jna.platform.win32.WinDef.HWND; import com.sun.jna.platform.win32.WinDef.RECT; public class JnaUtil { private static final User32 user32 = User32.INSTANCE; private static Pointer callBackHwnd; public static boolean windowExists(final String startOfWindowName) { return !user32.EnumWindows(new User32.WNDENUMPROC() { @Override public boolean callback(Pointer hWnd, Pointer userData) { byte[] windowText = new byte[512]; user32.GetWindowTextA(hWnd, windowText, 512); String wText = Native.toString(windowText).trim(); if (!wText.isEmpty() && wText.startsWith(startOfWindowName)) { return false; } return true; } }, null); } public static List<String> getAllWindowNames() { final List<String> windowNames = new ArrayList<String>(); user32.EnumWindows(new User32.WNDENUMPROC() { @Override public boolean callback(Pointer hWnd, Pointer arg) { byte[] windowText = new byte[512]; user32.GetWindowTextA(hWnd, windowText, 512); String wText = Native.toString(windowText).trim(); if (!wText.isEmpty()) { windowNames.add(wText); } return true; } }, null); return windowNames; } public static boolean windowExists(Pointer hWnd) { return user32.IsWindow(hWnd); } public static Pointer getWinHwnd(final String startOfWindowName) { callBackHwnd = null; user32.EnumWindows(new User32.WNDENUMPROC() { @Override public boolean callback(Pointer hWnd, Pointer userData) { byte[] windowText = new byte[512]; user32.GetWindowTextA(hWnd, windowText, 512); String wText = Native.toString(windowText).trim(); if (!wText.isEmpty() && wText.startsWith(startOfWindowName)) { callBackHwnd = hWnd; return false; } return true; } }, null); return callBackHwnd; } public static boolean setForegroundWindow(Pointer hWnd) { return user32.SetForegroundWindow(hWnd) != 0; } public static Pointer getForegroundWindow() { return user32.GetForegroundWindow(); } public static String getForegroundWindowText() { Pointer hWnd = getForegroundWindow(); int nMaxCount = 512; byte[] lpString = new byte[nMaxCount]; int getWindowTextResult = user32 .GetWindowTextA(hWnd, lpString, nMaxCount); if (getWindowTextResult == 0) { return ""; } return Native.toString(lpString); } public static boolean isForegroundWindow(Pointer hWnd) { return user32.GetForegroundWindow().equals(hWnd); } public static boolean setForegroundWindow(String startOfWindowName) { Pointer hWnd = getWinHwnd(startOfWindowName); return user32.SetForegroundWindow(hWnd) != 0; } public static Rectangle getWindowRect(Pointer hWnd) throws JnaUtilException { if (hWnd == null) { throw new JnaUtilException( "Failed to getWindowRect since Pointer hWnd is null"); } Rectangle result = null; RECT rect = new RECT(); boolean rectOK = user32.GetWindowRect(hWnd, rect); if (rectOK) { int x = rect.left; int y = rect.top; int width = rect.right - rect.left; int height = rect.bottom - rect.top; result = new Rectangle(x, y, width, height); } return result; } public static boolean setWindowPos(Pointer hWnd, int x, int y, int w, int h) { int uFlags = 0; return user32.SetWindowPos(hWnd, User32.HWND_TOP, x, y, w, h, uFlags); } public static boolean moveWindow(Pointer hWnd, int x, int y, int nWidth, int nHeight) { boolean bRepaint = true; return user32.MoveWindow(hWnd, x, y, nWidth, nHeight, bRepaint ); } public static Rectangle getWindowRect(String startOfWindowName) throws JnaUtilException { Pointer hWnd = getWinHwnd(startOfWindowName); if (hWnd != null) { return getWindowRect(hWnd); } else { throw new JnaUtilException("Failed to getWindowRect for \"" + startOfWindowName + "\""); } } public static Pointer getWindow(Pointer hWnd, int uCmd) { return user32.GetWindow(hWnd, uCmd); } public static String getWindowText(Pointer hWnd) { int nMaxCount = 512; byte[] lpString = new byte[nMaxCount]; int result = user32.GetWindowTextA(hWnd, lpString, nMaxCount); if (result == 0) { return ""; } return Native.toString(lpString); } public static Pointer getOwnerWindow(Pointer hWnd) { return user32.GetWindow(hWnd, User32.GW_OWNER); } public static String getOwnerWindow(String childTitle) { Pointer hWnd = getWinHwnd(childTitle); Pointer parentHWnd = getOwnerWindow(hWnd); if (parentHWnd == null) { return ""; } return getWindowText(parentHWnd); } public static Pointer getNextWindow(Pointer hWnd) { if (hWnd == null) { return null; } return user32.GetWindow(hWnd, User32.GW_HWNDNEXT); } public static boolean isWindowVisible(Pointer hWnd) { return user32.IsWindowVisible(hWnd); } public static Pointer getParent(Pointer hWnd) { return user32.GetParent(hWnd); } public static Pointer getRoot(Pointer hWnd) { return user32.GetAncestor(hWnd, User32.GA_ROOT); } public static LONG_PTR getWindowLongPtr(Pointer hWndP, int nIndex) { HWND hwnd = new HWND(hWndP); return user32.GetWindowLongPtr(hwnd, nIndex); }
JnaUtilException.java
public class JnaUtilException extends Exception { private static final long serialVersionUID = 1L; public JnaUtilException(String text) { super(text); } }