模拟键盘的动作信号-"SendInput示例"

(1)定义一个引用类(核心类)

 1 using System;
 2 using System.Collections.Generic;
 3 using System.Linq;
 4 using System.Text;
 5 using System.Threading.Tasks;
 6 
 7 using System.Runtime.InteropServices;
 8 
 9 namespace VirtualKey
10 {
11     class DLL_Navigation
12     {
13         public const int WM_KEYDOWN = 0x0100;
14         public const int WM_KEYUP = 0x0101;
15         public const int WM_CHAR = 0x0106;
16         public const int WM_SYSKEYDOWN = 0x0104;
17         public const int WM_SYSKEYUP = 0x0105;
18 
19         public const int VK_MENU = 0x12;    //ALT   键
20         public const int VK_LMENU = 0xA4;    //LALT   键
21         public const int Key_T = 0x54;      //T     键
22         public const int Key_Q = 0x51;      //Q     键
23         public const int Key_C = 0x43;      //C     键
24         public const int Key_F = 0x46;      //F     键
25         public const int Key_P = 0x50;      //P     键
26         public const int VK_LWIN = 0x5B;    //VK_LWIN   键
27         public const int Key_D = 0x44;      //D     键
28          public const int Key_E = 0x45;      //E     键
29 
30         [DllImport("user32.dll")]
31         public static extern IntPtr FindWindowA(string lpClassName, string lpWindowName);
32 
33         [DllImport("user32.dll")]
34         public static extern bool ShowWindow(IntPtr hwnd, int nCmdShow);
35 
36         [DllImport("user32.dll")]
37         public static extern bool SendMessageA(IntPtr hwnd, uint msg, int wParam, uint lParam);
38 
39         [DllImport("user32.dll")]
40         public static extern IntPtr GetForegroundWindow(); //获得本窗体的句柄
41         [DllImport("user32.dll")]
42         public static extern bool SetForegroundWindow(IntPtr hWnd); //设置此窗体为活动窗体
43 
44         [DllImport("user32.dll")]
45         public static extern uint SendInput(uint cInputs, Input[] inputs, int size);
46 
47         public enum INPUT_TYPE { INPUT_MOUSE = 0, INPUT_KEYBOARD, INPUT_HARDWARE }
48         public enum KEYEVENTF { KEYEVENTF_EXTENDEDKEY = 0x0001, KEYEVENTF_KEYUP = 0x0002, KEYEVENTF_SCANCODE = 0x0008, KEYEVENTF_UNICODE = 0x0004 };
49 
50         [StructLayout(LayoutKind.Explicit)]
51         public struct Input
52         {
53             //x64 - 8, x32 - 4
54             [FieldOffset(0)] public INPUT_TYPE type;
55             [FieldOffset(4)] public MouseInput mi;
56             [FieldOffset(4)] public tagKEYBDINPUT ki;
57             [FieldOffset(4)] public tagHARDWAREINPUT hi;
58         }
59         [StructLayout(LayoutKind.Sequential)]
60         public struct MouseInput
61         {
62             public Int32 dx;
63             public Int32 dy;
64             public Int32 Mousedata;
65             public Int32 dwFlag;
66             public Int32 time;
67             public IntPtr dwExtraInfo;
68         }
69         [StructLayout(LayoutKind.Sequential)]
70         public struct tagKEYBDINPUT
71         {
72             public Int16 wVk;
73             public Int16 wScan;
74             public Int32 dwFlags;
75             public Int32 time;
76             public IntPtr dwExtraInfo;
77         }
78 
79         [StructLayout(LayoutKind.Sequential)]
80         public struct tagHARDWAREINPUT
81         {
82             public Int32 uMsg;
83             public Int16 wParamL;
84             public Int16 wParamH;
85         }
86     }
87 }

(2)应用类

 1 using System;
 2 using System.Collections.Generic;
 3 using System.ComponentModel;
 4 using System.Data;
 5 using System.Drawing;
 6 using System.Linq;
 7 using System.Text;
 8 using System.Threading.Tasks;
 9 using System.Windows.Forms;
10 
11 using System.Diagnostics;
12 using System.Runtime.InteropServices;
13 
14 namespace VirtualKey
15 {
16     public partial class Form1 : Form
17     {
18         public Form1()
19         {
20             InitializeComponent();
21             
22         }
23 
24         //UI 添加一个按钮,名称为:btn_Send
25         private void btn_Send_Click(object sender, EventArgs e)
26         {
27             SendMsg();
28         }
29 
30         //SendInput示例:
31         //1.找到目标窗体句柄Obj
32         //2.令目标窗体处于前置状态
33         //3.构建待发送的模拟键盘信号:ALT+T+Q
34         //4.执行发送动作
35         private void SendMsg()
36         {
37             //IntPtr hWnd = DLL_Navigation.FindWindowA("TMainForm", "TS");//通过"Microsoft Spy++"确定参数1的值。参数1可以为空。
38             IntPtr hWnd = DLL_Navigation.FindWindowA(null, "TS");
39             if (hWnd == null)
40             {
41                 Console.WriteLine("未找到指定程序窗体!");
42                 return;
43             }
44 
45             //DLL_Navigation.ShowWindow(hWnd, 1); //如果无需显示窗体,可以省略此语句。
46             if(hWnd!=DLL_Navigation.GetForegroundWindow())
47             {
48                 DLL_Navigation.SetForegroundWindow(hWnd);
49             }
50 
51             DLL_Navigation.Input[] inputs=new DLL_Navigation.Input[6];
52             inputs[0].type = DLL_Navigation.INPUT_TYPE.INPUT_KEYBOARD;
53             inputs[0].ki.wVk = DLL_Navigation.VK_LMENU;
54         
55             inputs[1].type = DLL_Navigation.INPUT_TYPE.INPUT_KEYBOARD;
56             inputs[1].ki.wVk = DLL_Navigation.Key_T;
57 
58             inputs[2].type = DLL_Navigation.INPUT_TYPE.INPUT_KEYBOARD;
59             inputs[2].ki.wVk = DLL_Navigation.Key_T;
60             inputs[2].ki.dwFlags = (int)DLL_Navigation.KEYEVENTF.KEYEVENTF_KEYUP;
61 
62             inputs[3].type = DLL_Navigation.INPUT_TYPE.INPUT_KEYBOARD;
63             inputs[3].ki.wVk = DLL_Navigation.Key_Q;
64 
65             inputs[4].type = DLL_Navigation.INPUT_TYPE.INPUT_KEYBOARD;
66             inputs[4].ki.wVk = DLL_Navigation.Key_Q;
67             inputs[4].ki.dwFlags = (int)DLL_Navigation.KEYEVENTF.KEYEVENTF_KEYUP;
68 
69             inputs[5].type = DLL_Navigation.INPUT_TYPE.INPUT_KEYBOARD;
70             inputs[5].ki.wVk = DLL_Navigation.VK_LMENU;
71             inputs[5].ki.dwFlags = (int)DLL_Navigation.KEYEVENTF.KEYEVENTF_KEYUP;
72 
73             uint uSent = DLL_Navigation.SendInput((uint)inputs.Length, inputs, Marshal.SizeOf(inputs[0]));
74        if(uSent!=(uint)inputs.Length)
         {Console.WriteLine("按键命令发送失败!");}
inputs = null;
75         }
76     }
77 }

 

posted @ 2022-12-30 16:13  冲云霄  阅读(2)  评论(0)    收藏  举报