我正在尝试使用Postmessage将键击发送到特定窗口,但它似乎对我不起作用。这是我的代码
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);
}
}
当我改为
时,它也无效 User32.INSTANCE.PostMessage(hwnd, WM_KEYDOWN, wPARAM, lPARAM);
User32.INSTANCE.PostMessage(hwnd, WM_KEYUP, wPARAM, lPARAM);