Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import com.sun.jna.platform.win32.User32;
- import com.sun.jna.platform.win32.WinDef;
- import com.sun.jna.platform.win32.WinUser;
- import com.sun.jna.Native;
- import com.sun.jna.platform.win32.Kernel32;
- public class InputSender {
- public static void main(String[] args) {
- int processId = 1234; // Replace with your target process ID
- WinDef.HWND hwnd = findWindowByProcessId(processId);
- if (hwnd != null) {
- User32.INSTANCE.SetForegroundWindow(hwnd);
- sendKeys("Hello from Java!");
- moveMouse(100, 200); // Move mouse to coordinates (100, 200)
- clickMouse(); // Perform a mouse click
- } else {
- System.out.println("Window not found!");
- }
- }
- public static void setWindowPosition(WinDef.HWND hwnd, int x, int y, int width, int height) {
- // SWP_NOZORDER maintains the window's z-order position
- User32.INSTANCE.SetWindowPos(hwnd, null, x, y, width, height, WinUser.SWP_NOZORDER);
- }
- private static WinDef.HWND findWindowByProcessId(int processId) {
- final WinDef.HWND[] result = {null};
- User32.INSTANCE.EnumWindows((hwnd, pointer) -> {
- byte[] windowText = new byte[512];
- User32.INSTANCE.GetWindowTextA(hwnd, windowText, 512);
- int[] pid = new int[1];
- User32.INSTANCE.GetWindowThreadProcessId(hwnd, pid);
- if (pid[0] == processId) {
- result[0] = hwnd;
- return false; // Stop enumeration
- }
- return true; // Continue enumeration
- }, null);
- return result[0];
- }
- private static void sendKeys(String data) {
- for (char c : data.toCharArray()) {
- short vk = (short) Character.toUpperCase(c);
- User32.INSTANCE.keybd_event(vk, (byte) 0, WinUser.KEYBD_EVENTF_KEYDOWN, null);
- try {
- Thread.sleep(10); // small pause between key presses
- } catch (InterruptedException e) {
- e.printStackTrace();
- }
- User32.INSTANCE.keybd_event(vk, (byte) 0, WinUser.KEYBD_EVENTF_KEYUP, null);
- }
- }
- private static void moveMouse(int x, int y) {
- User32.INSTANCE.SetCursorPos(x, y);
- }
- private static void clickMouse() {
- User32.INSTANCE.mouse_event(WinUser.MOUSEEVENTF_LEFTDOWN, 0, 0, 0, null);
- User32.INSTANCE.mouse_event(WinUser.MOUSEEVENTF_LEFTUP, 0, 0, 0, null);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement