响应快捷键,发送指定字符串.(WINDOWS消息,非底层)

 ///注册函数,响应快捷键

        [DllImport("user32.dll")]
        public static extern bool RegisterHotKey(IntPtr wnd, int id, MODKEY mode, Keys vk);
        [DllImport("user32.dll")]
        public static extern bool UnregisterHotKey(IntPtr wnd, int id);
        [Flags()]
        public enum MODKEY { None = 0, ALT = 0x0001, CTRL = 0x0002, SHIFT = 0x0004, WIN = 0x0008, }
        public string mystring = "";
        public Form1()
        {
            InitializeComponent();
            RegisterHotKey(this.Handle, 10, MODKEY.ALT, Keys.R);
            RegisterHotKey(this.Handle, 11, MODKEY.ALT, Keys.Q);
        }

      
        void sendchar(string str)   //发送字符串
        {
            foreach (var s in str)
            { SendKeys.Send(string.Format("{0}", s)); }
        
        
        }
        protected override void WndProc(ref Message m)
        {
            if (m.Msg == 0x0312)
            {
                switch (m.WParam.ToInt32())
                {
                    case 10:
                        sendchar(mystring);
                        break;
                    case 11:

                        break;
                    default:
                        break;
                }
                return;
            }
            base.WndProc(ref m);
        }

///问题:自定义快捷键,底层模拟按键

//SendKeys.Send // 异步模拟按键(不阻塞UI)

//SendKeys.SendWait // 同步模拟按键(会阻塞UI直到对方处理完消息后返回)

posted @ 2018-08-16 16:55  clarklxr  阅读(114)  评论(0编辑  收藏  举报