0

I am trying to send a keystroke to a specific window using Postmessage, but it seems not works for me. this is my codes

import com.sun.jna.platform.win32.User32;
import com.sun.jna.platform.win32.WinDef.HWND;
import com.sun.jna.platform.win32.WinDef.LPARAM;
import com.sun.jna.platform.win32.WinDef.WPARAM;


public class keystroke {

   public static void main(String[] args){

      int WM_KEYDOWN = 0x0100;
      int WM_KEYUP = 0x0101;
      int WM_CHAR = 0x0102;

      int VK_A = 0x41;


      WPARAM wPARAM = new WPARAM(VK_A);
      LPARAM lPARAM = new LPARAM(0);

      HWND hwnd = User32.INSTANCE.FindWindow(null, "Notepad"); // window title

      User32.INSTANCE.ShowWindow(hwnd, 9); // SW_RESTOR
      User32.INSTANCE.SetForegroundWindow(hwnd); // bring to front

      User32.INSTANCE.SetFocus(hwnd);

      User32.INSTANCE.PostMessage(hwnd, WM_CHAR, wPARAM, lPARAM);

     }
}

it also not works when i change to this

  User32.INSTANCE.PostMessage(hwnd, WM_KEYDOWN, wPARAM, lPARAM);
  User32.INSTANCE.PostMessage(hwnd, WM_KEYUP, wPARAM, lPARAM);
gprathour
  • 14,813
  • 5
  • 66
  • 90
user3789645
  • 91
  • 1
  • 5

0 Answers0