首页 > 编程语言 >java 模拟输入

java 模拟输入

时间:2023-02-01 12:04:28浏览次数:41  
标签:WinDef java ki DWORD 模拟 new input INPUT 输入


package cn.net.haotuo;

import com.sun.jna.platform.win32.BaseTSD;
import com.sun.jna.platform.win32.User32;
import com.sun.jna.platform.win32.WinDef;
import com.sun.jna.platform.win32.WinUser;


/**
* Created by lenovo on 2017/4/27.
* 使用winID来获得窗口的类型和标题,然后发送消息或者其他操作
*/
public class POPO {
public static void main(String[] args) {

WinDef.HWND hwnd = User32.INSTANCE.FindWindow
("Notepad", null); // 第一个参数是Windows窗体的窗体类,第二个参数是窗体的标题。不熟悉windows编程的需要先找一些Windows窗体数据结构的知识来看看,还有windows消息循环处理,其他的东西不用看太多。
if (hwnd == null) {
System.out.println("Excel is not running");
} else {
User32.INSTANCE.ShowWindow(hwnd, 9); // SW_RESTORE
User32.INSTANCE.SetForegroundWindow(hwnd); // bring to front
User32.INSTANCE.SetFocus(hwnd);
String username = "123";
for (Character c : username.toCharArray()) {
sendChar(c);
sendCharPress(c);
sendCharRelease(c);
}


}
}

static WinUser.INPUT input = new WinUser.INPUT();

static void sendChar(char ch) {

input.type = new WinDef.DWORD(WinUser.INPUT.INPUT_KEYBOARD);
input.input.setType("ki"); // Because setting INPUT_INPUT_KEYBOARD is not enough: https://groups.google.com/d/msg/jna-users/NDBGwC1VZbU/cjYCQ1CjBwAJ
input.input.ki.wScan = new WinDef.WORD(0);
input.input.ki.time = new WinDef.DWORD(0);
input.input.ki.dwExtraInfo = new BaseTSD.ULONG_PTR(0);
// Press
input.input.ki.wVk = new WinDef.WORD(Character.toUpperCase(ch)); // 0x41
input.input.ki.dwFlags = new WinDef.DWORD(0); // keydown
User32.INSTANCE.SendInput(new WinDef.DWORD(1), (WinUser.INPUT[]) input.toArray(1), input.size());

// Release
input.input.ki.wVk = new WinDef.WORD(Character.toUpperCase(ch)); // 0x41
input.input.ki.dwFlags = new WinDef.DWORD(2); // keyup

User32.INSTANCE.SendInput(new WinDef.DWORD(1), (WinUser.INPUT[]) input.toArray(1), input.size());

}


static void sendCharPress(char ch) {

input.type = new WinDef.DWORD(WinUser.INPUT.INPUT_KEYBOARD);
input.input.setType("ki"); // Because setting INPUT_INPUT_KEYBOARD is not enough: https://groups.google.com/d/msg/jna-users/NDBGwC1VZbU/cjYCQ1CjBwAJ
input.input.ki.wScan = new WinDef.WORD(0);
input.input.ki.time = new WinDef.DWORD(0);
input.input.ki.dwExtraInfo = new BaseTSD.ULONG_PTR(0);
// Press
input.input.ki.wVk = new WinDef.WORD(Character.toUpperCase(ch)); // 0x41
input.input.ki.dwFlags = new WinDef.DWORD(0); // keydown
User32.INSTANCE.SendInput(new WinDef.DWORD(1), (WinUser.INPUT[]) input.toArray(1), input.size());

}

static void sendCharRelease(char ch) {

input.type = new WinDef.DWORD(WinUser.INPUT.INPUT_KEYBOARD);
input.input.setType("ki"); // Because setting INPUT_INPUT_KEYBOARD is not enough: https://groups.google.com/d/msg/jna-users/NDBGwC1VZbU/cjYCQ1CjBwAJ
input.input.ki.wScan = new WinDef.WORD(0);
input.input.ki.time = new WinDef.DWORD(0);
input.input.ki.dwExtraInfo = new BaseTSD.ULONG_PTR(0);


// Release
input.input.ki.wVk = new WinDef.WORD(Character.toUpperCase(ch)); // 0x41
input.input.ki.dwFlags = new WinDef.DWORD(2); // keyup

User32.INSTANCE.SendInput(new WinDef.DWORD(1), (WinUser.INPUT[]) input.toArray(1), input.size());

}
}


标签:WinDef,java,ki,DWORD,模拟,new,input,INPUT,输入
From: https://blog.51cto.com/u_10780206/6031175

相关文章

  • Illustrator JavaScript 出血
    IllustratorJavaScript出血vardoc=app.activeDocument;varpt=72/25.4;varrc=1*pt;varls=6*pt;for(vari=0;i<doc.selection.length;i++){varshape=d......
  • Java 创建一个快捷窗口 用于监控文件夹与打开文件夹
    样式代码结构全部代码importjavax.swing.*;importjava.awt.*;importjava.awt.event.ActionEvent;importjava.awt.event.ActionListener;importjava.io.File;importjav......
  • java synchronized wait notifyAll Thread
    虚假唤醒ifpackagepc;importjava.util.ArrayList;importjava.util.List;importjava.util.Random;publicclassD1{publicstaticvoidmain(String[]args){......
  • java 读写文本
    /****@parampath*@paramcharsetNameutf-8gbk*@return*/publicstaticList<String>readtFile(Stringpath,StringcharsetName){......
  • java字符串拼接逗号分隔
    List<String>a=newArrayList<>();a.add("123");//a.add("3213");//a.add("3213");//a.add("213");//a.add("1......
  • JavaScript奇技淫巧:操控URL
    JavaScript奇技淫巧:操控URL本文展示两种不常见的JS编程技巧:实现操控浏览器窗口,更改父窗口和子窗口的URL地址。修改父窗口URL当使用window.open()打开一个窗口,可以用window.......
  • Guava:google公司开发的一款Java类库扩展工具包
    Guava是google公司开发的一款Java类库扩展工具包文档https://github.com/google/guavahttps://github.com/google/guava/wikihttps://guava.dev/releases/snapshot-jre......
  • 面试官:你说说 JavaScript 中类型的转换机制
    前言面试官:“你说说JavaScript中类型的转换机制”紧张的萌新:“字符串跟其他类型拼接会变成字符串...”面试官:“...”······大家好,我是CoderBin。又来到了面试官......
  • Java中的对集合便利的方式有区别吗
    提问: 问题遇到的现象和发生背景在学习mybatis遍历结果的时候,突然想起有好多种遍历方式,但是我本身并不清楚这些遍历的原理,很难理解他们之间的不同用代码块功能插入代码,请......
  • java对方法进行反射
    1.获取类的class信息2.获取method3.invoke方法执行method 被反射对象```@Data@AllArgsConstructor@NoArgsConstructorpublicclassUser{privateStringid;......