WPF 简单使用keybd_event模拟触发键盘

主要是添加Win32函数

其次是定义键盘按下,抬起的两个固定值。

        [DllImport("User32.dll")]
        public static extern void keybd_event(byte bVk, byte bScan, int dwFlags, int dwExtraInfo);
        /// <summary>
        /// 按下
        /// </summary>
        const int KEYEVENTF_KEYDOWN = 0;
        /// <summary>
        /// 抬起
        /// </summary>
        const int KEYEVENTF_KEYUP = 0x2;

 

其次使用的时候是可以使用net自带的Keys的枚举值

 

比如说模拟发送:Ctrl+A Ctrl+C这两个按键的过程

            keybd_event((byte)(Keys.LControlKey), 0, KEYEVENTF_KEYDOWN, 0);//按下ctrl
            keybd_event((byte)(Keys.A), 0, KEYEVENTF_KEYDOWN, 0);//按下A
            keybd_event((byte)Keys.A, 0, KEYEVENTF_KEYUP, 0);//抬起A
            keybd_event((byte)Keys.LControlKey, 0, KEYEVENTF_KEYUP, 0);//抬起ctrl 
            keybd_event((byte)(Keys.LControlKey), 0, KEYEVENTF_KEYDOWN, 0);
            keybd_event((byte)(Keys.C), 0, KEYEVENTF_KEYDOWN, 0);
            keybd_event((byte)Keys.C, 0, KEYEVENTF_KEYUP, 0);
            keybd_event((byte)Keys.LControlKey, 0, KEYEVENTF_KEYUP, 0);

 

posted @ 2021-02-20 00:23  ARM830  阅读(592)  评论(0编辑  收藏  举报