<dependency>
<groupId>net.java.dev.jna</groupId>
<artifactId>jna</artifactId>
<version>5.5.0</version>
</dependency>
<dependency>
<groupId>net.java.dev.jna</groupId>
<artifactId>jna-platform</artifactId>
<version>5.5.0</version>
</dependency>
1 先获取窗口句柄
WinDef.HWND hwnd = User32.INSTANCE.FindWindow(null, "QQ"); // 第一个参数是Windows窗体的窗体类,第二个参数是窗体的标题
2 再设置活动窗口
User32.INSTANCE.SetForegroundWindow(hwnd);
1 键码表
KeyEvent.VK_ENTER //代表回车
presskey(KeyEvent.VK_A); //代表按下A参数换成65即A的ascall码也行
2 具体实现按下的封装
public static void presskey(char keycode) {
robot.keyPress(keycode);
robot.keyRelease(keycode);
}
String s = "奥利给,铁子们";
for (int i = 0; i < s.length(); i++) {
ch = s.charAt(i);
User32.INSTANCE.SetForegroundWindow(hwnd);
User32.INSTANCE.PostMessage(hwnd, WM_CHAR, new WinDef.WPARAM(ch), new WinDef.LPARAM(ch));
}