SimulateInput.cs 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Runtime.InteropServices;
  5. using System.Text;
  6. using System.Threading.Tasks;
  7. internal class SimulateInput
  8. {
  9. // 定义 INPUT 结构 (用于 SendInput)
  10. [StructLayout(LayoutKind.Sequential)]
  11. public struct INPUT
  12. {
  13. public uint type;
  14. public INPUTUNION u;
  15. }
  16. [StructLayout(LayoutKind.Explicit)]
  17. public struct INPUTUNION
  18. {
  19. [FieldOffset(0)]
  20. public MOUSEINPUT mi;
  21. [FieldOffset(0)]
  22. public KEYBDINPUT ki;
  23. [FieldOffset(0)]
  24. public HARDWAREINPUT hi;
  25. }
  26. [StructLayout(LayoutKind.Sequential)]
  27. public struct KEYBDINPUT
  28. {
  29. public ushort wVk;
  30. public ushort wScan;
  31. public uint dwFlags;
  32. public uint time;
  33. public IntPtr dwExtraInfo;
  34. }
  35. [StructLayout(LayoutKind.Sequential)]
  36. public struct MOUSEINPUT
  37. {
  38. public int dx;
  39. public int dy;
  40. public uint mouseData;
  41. public uint dwFlags;
  42. public uint time;
  43. public IntPtr dwExtraInfo;
  44. }
  45. [StructLayout(LayoutKind.Sequential)]
  46. public struct HARDWAREINPUT
  47. {
  48. public uint uMsg;
  49. public ushort wParamL;
  50. public ushort wParamH;
  51. }
  52. // 常量
  53. private const uint INPUT_KEYBOARD = 1;
  54. private const uint KEYEVENTF_KEYUP = 0x0002;
  55. public const ushort VK_LWIN = 0x5B; // 左 Windows 键
  56. public const ushort VK_RWIN = 0x5C; // 右 Windows 键
  57. public const ushort VK_BACKSPACE = 0x08; // 后退
  58. public const ushort VK_UP = 0x26; // 上
  59. public const ushort VK_DOWN = 0x28; // 下
  60. public const ushort VK_NUM0 = 0x30;
  61. public const ushort VK_NUM1 = 0x31;
  62. public const ushort VK_NUM2 = 0x32;
  63. public const ushort VK_NUM3 = 0x33;
  64. public const ushort VK_NUM4 = 0x34;
  65. public const ushort VK_NUM5 = 0x35;
  66. public const ushort VK_NUM6 = 0x36;
  67. public const ushort VK_NUM7 = 0x37;
  68. public const ushort VK_NUM8 = 0x38;
  69. public const ushort VK_NUM9 = 0x39;
  70. public const ushort VK_DOT = 0xBE;
  71. // 导入 SendInput 函数
  72. [DllImport("user32.dll", SetLastError = true)]
  73. static extern uint SendInput(uint nInputs, ref INPUT pInputs, int cbSize);
  74. public static void SimulateKey(ushort keyCode)
  75. {
  76. INPUT input = new INPUT();
  77. input.type = INPUT_KEYBOARD;
  78. input.u.ki.wVk = keyCode; // 使用左 Windows 键
  79. input.u.ki.wScan = 0; // 虚拟键码模式,wScan 可为 0
  80. input.u.ki.dwFlags = 0; // 按下 (keydown)
  81. input.u.ki.time = 0;
  82. input.u.ki.dwExtraInfo = IntPtr.Zero;
  83. // 发送 "按下" 事件
  84. SendInput(1, ref input, Marshal.SizeOf(typeof(INPUT)));
  85. // 可选:短暂延迟 (模拟按键时长)
  86. // System.Threading.Thread.Sleep(50);
  87. // 发送 "释放" 事件
  88. input.u.ki.dwFlags = KEYEVENTF_KEYUP; // 释放 (keyup)
  89. SendInput(1, ref input, Marshal.SizeOf(typeof(INPUT)));
  90. }
  91. }