Advertisement
mahldcat

SendInput.java

Sep 8th, 2024 (edited)
56
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.48 KB | None | 0 0
  1. import com.sun.jna.platform.win32.User32;
  2. import com.sun.jna.platform.win32.WinDef;
  3. import com.sun.jna.platform.win32.WinUser;
  4. import com.sun.jna.Native;
  5. import com.sun.jna.platform.win32.Kernel32;
  6.  
  7. public class InputSender {
  8.  
  9.     public static void main(String[] args) {
  10.         int processId = 1234; // Replace with your target process ID
  11.         WinDef.HWND hwnd = findWindowByProcessId(processId);
  12.         if (hwnd != null) {
  13.             User32.INSTANCE.SetForegroundWindow(hwnd);
  14.             sendKeys("Hello from Java!");
  15.             moveMouse(100, 200);  // Move mouse to coordinates (100, 200)
  16.             clickMouse();         // Perform a mouse click
  17.         } else {
  18.             System.out.println("Window not found!");
  19.         }
  20.     }
  21.  
  22.     public static void setWindowPosition(WinDef.HWND hwnd, int x, int y, int width, int height) {
  23.         // SWP_NOZORDER maintains the window's z-order position
  24.         User32.INSTANCE.SetWindowPos(hwnd, null, x, y, width, height, WinUser.SWP_NOZORDER);
  25.     }
  26.  
  27.     private static WinDef.HWND findWindowByProcessId(int processId) {
  28.         final WinDef.HWND[] result = {null};
  29.         User32.INSTANCE.EnumWindows((hwnd, pointer) -> {
  30.             byte[] windowText = new byte[512];
  31.             User32.INSTANCE.GetWindowTextA(hwnd, windowText, 512);
  32.             int[] pid = new int[1];
  33.             User32.INSTANCE.GetWindowThreadProcessId(hwnd, pid);
  34.             if (pid[0] == processId) {
  35.                 result[0] = hwnd;
  36.                 return false; // Stop enumeration
  37.             }
  38.             return true; // Continue enumeration
  39.         }, null);
  40.         return result[0];
  41.     }
  42.  
  43.     private static void sendKeys(String data) {
  44.         for (char c : data.toCharArray()) {
  45.             short vk = (short) Character.toUpperCase(c);
  46.             User32.INSTANCE.keybd_event(vk, (byte) 0, WinUser.KEYBD_EVENTF_KEYDOWN, null);
  47.             try {
  48.                 Thread.sleep(10); // small pause between key presses
  49.             } catch (InterruptedException e) {
  50.                 e.printStackTrace();
  51.             }
  52.             User32.INSTANCE.keybd_event(vk, (byte) 0, WinUser.KEYBD_EVENTF_KEYUP, null);
  53.         }
  54.     }
  55.  
  56.     private static void moveMouse(int x, int y) {
  57.         User32.INSTANCE.SetCursorPos(x, y);
  58.     }
  59.  
  60.     private static void clickMouse() {
  61.         User32.INSTANCE.mouse_event(WinUser.MOUSEEVENTF_LEFTDOWN, 0, 0, 0, null);
  62.         User32.INSTANCE.mouse_event(WinUser.MOUSEEVENTF_LEFTUP, 0, 0, 0, null);
  63.     }
  64. }
  65.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement