Home » Java » java sending keystrokes using robot class

java sending keystrokes using robot class

Posted by: admin December 28, 2021 Leave a comment

Questions:

I know it’s possible to send keystrokes in java using Robot Class , but is there any way to specify the target process when it’s already started ?

Answers:

The Robot will send the keystrokes to whichever application window is on top. To send keystrokes to a specific target, you will want to set the target as the platform’s foreground window first. Doing this may require native code such as that provided by JNI or (what I use) JNA. If you desire to send keystrokes to a background window, I believe that you cannot use Robot, that you will have to write native code. Of course all native code solutions will be platform dependent.


You ask:

Would you please give me some more details on how to do it with JNA
sorry , I’m writing for windows

For Windows, you would interface with the user32.dll library. Something like this has 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;

/**
 * JNA interface with Window's user32.dll
 * 
 * @author Pete S
 * 
 */
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; // used with GetWindow to get win owner
   public static final int GW_HWNDNEXT = 2; // used with GetNextWindow
   public static final int GA_ROOT = 2; // used with GetAncestor
   public static final int GWL_EXSTYLE = -20; // used with GetWindowLong
   public static final long WS_EX_APPWINDOW = 0x00040000L;
   public static final Pointer HWND_TOP = new Pointer(0L); // used with
                                                           // SetWindowPos

   boolean EnumWindows(WNDENUMPROC lpEnumFunc, Pointer userData);

   int GetWindowTextA(Pointer hWnd, byte[] lpString, int nMaxCount);

   int SetForegroundWindow(Pointer hWnd);

   Pointer GetForegroundWindow();

   boolean GetWindowRect(Pointer hWnd, RECT rect);

   boolean SetWindowPos(Pointer hWnd, Pointer hWndInsertAfter, int x, int y,
         int cx, int cy, int uFlags);

   boolean MoveWindow(Pointer hWnd, int x, int y, int nWidth, int nHeight, boolean bRepaint);

   boolean IsWindow(Pointer hWnd);

   Pointer GetWindow(Pointer hWnd, int uCmd);

   LONG_PTR GetWindowLongPtr(HWND hWnd, int nIndex);

   Pointer GetParent(Pointer hWnd);

   Pointer GetAncestor(Pointer hWnd, int gaFlags);

   boolean IsWindowVisible(Pointer hWnd);
}

JnaUtil.java

JnaUtilException.java