• java 模拟输入


    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());
    
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87
    • 88
  • 相关阅读:
    好用的博客评论系统 Valine 使用及避坑指南
    基于C++11的数据库连接池(推荐)
    ubuntu 18.04安装python3.7.5,并将 python 设定为python3, pip设定为pip3
    静态代理和动态代理
    FFmpeg 命令:从入门到精通 | ffppeg 命令参数说明
    读书笔记<高速上手C11 14 17>
    高阶数据结构学习之图
    让学前端不再害怕英语单词(一)
    【斗罗二】再度魔改,弓老被删,冰帝武魂提前曝光,官方经费已不足
    【论文笔记】Scene as Occupancy
  • 原文地址:https://blog.csdn.net/jialan75/article/details/126976564