AlexSJC
发布于 2023-06-24 / 5 阅读
0
0

C# 模拟按键

#C#
public class KeySim
{
    [DllImport("user32.dll")]
    private static extern void keybd_event(byte bVk, byte bScan, int dwFlags, int dwExtraInfo);
 
    public static void Press(string keys)
    {
        Press(keys.Split('+')
            .Select(s => (Keys)Enum.Parse(typeof(Keys), s))
            .ToList());
    }
 
    public static void Press(List<Keys> keys)
    {
        keys.ForEach(k => keybd_event((byte)k, 0, 0, 0));
        Thread.Sleep(1000);
        keys.ForEach(k => keybd_event((byte)k, 0, 2, 0));
    }
}
KeySim.Press("LMenu+A");
KeySim.Press(new List<Keys> { Keys.LMenu, Keys.A });


评论