在C#中模拟按键输入可以通过调用Windows API函数来实现。下面是一个简单的示例代码,可以模拟按下和释放某个键的操作: ```c# using System; using System.Runtime.InteropServices; using System.Windows.Forms; public class KeyboardSimulator { [DllImport("user32.dll", SetLastError = true)] static extern void keybd_event(byte bVk, byte bScan, uint dwFlags, int dwExtraInfo); private const int KEYEVENTF_EXTENDEDKEY = 0x1; private const int KEYEVENTF_KEYUP = 0x2; private const int VK_SPACE = 32; public static void PressKey(Keys key) { keybd_event((byte)key, 0x45, KEYEVENTF_EXTENDEDKEY | 0, 0); } public static void ReleaseKey(Keys key) { keybd_event((byte)key, 0x45, KEYEVENTF_EXTENDEDKEY | KEYEVENTF_KEYUP, 0); } } ``` 使用方法: ```c# KeyboardSimulator.PressKey(Keys.A); // 模拟按下A键 KeyboardSimulator.ReleaseKey(Keys.A); // 模拟释放A键 ``` 该代码需要在Windows操作系统上运行,并且需要引用`System.Windows.Forms`命名空间。使用该代码可能会对正在运行的应用程序产生干扰,因此请谨慎使用。 [2024-01-17 16:55:10 | AI写代码神器 | 356点数解答]