[C#]使用API 获取设置系统热键和快捷键

     小颗豆的思路是:想要编一个小窗口程序,功能是打开某游戏后,按某个键然后鼠标自动重复点击游戏某个地方,再按某个键鼠标停止点击,这个问题涉及到全局键盘的捕获和释放,然后才是模拟鼠标(或键盘)按键
第一部分:先说捕获键盘某键,即获取设置系统热键和快捷键
    首先引用空间为
using System.Runtime.InteropServices;
    (一)第一种方法:
    先导入API:
        //注册热键的api
        [DllImport("user32")]
        public static extern bool RegisterHotKey(IntPtr hWnd, int id, uint control, Keys vk);//注册热键的api(注意这里是返回逻辑值,参数为句柄,热键ID,组合键,键符)
        //解除注册热键的api
        [DllImport("user32")]
        public static extern bool UnregisterHotKey(IntPtr hWnd, int id);

    调用API:
下边这段用于注册热键的两句可以放到某子程序先运行
            RegisterHotKey(this.Handle, 8888, 0, Keys.F4);//注册热键
            RegisterHotKey(this.Handle, 9999, 0, Keys.F5);
        //捕获键盘事件
        protected override void WndProc(ref Message m)
        {
            switch (m.Msg)
            {
                case 0x0312:    //这个是window消息定义的   注册的热键消息
                    if (m.WParam.ToString().Equals("8888"))  //这是我们注册的那個热键
                    {
                        action();//开始工作自己去写......
                    }
                    if (m.WParam.ToString().Equals("9999"))
                    {
                        stop();//停止工作自己去写......
                    }               
                   break;
            }
            base.WndProc(ref m);
        }
    释放热键的语句可以放到formleaveg事件中去处理
        private void Form1_Leave(object sender, EventArgs e)
        {
            UnregisterHotKey(this.Handle, 888);//释放热键
            UnregisterHotKey(this.Handle, 999);
        }

    (二)第二种方法:
    导入API跟上边有点不同:
        [DllImport("user32.dll")]
        private static extern int RegisterHotKey(IntPtr hwnd, int id, int fsModifiers, int vk);(注意这里返回的是整数,参数略有不同:要定义热键的窗口的句柄,热键ID不能与其它ID重复,组合键(没有时为0),热键值)
        [DllImport("user32.dll")]
        private static extern int UnregisterHotKey(IntPtr hwnd, int id);
     调用API:
        /// <summary>
        /// 注册热键
        /// </summary>
        /// <param name="hwnd">窗口句柄</param>
        /// <param name="hotKey_id">热键ID</param>
        /// <param name="fsModifiers">组合键(没有为0)</param>
        /// <param name="vk">热键</param>
        public bool RegKey(IntPtr hwnd, int hotKey_id, int fsModifiers, int vk)
        {
            bool result;
            if (RegisterHotKey(hwnd, hotKey_id, fsModifiers, vk) == 0)//注册失败返回0
            {
                result = false;
            }
            else
            {
                result = true;
            }
            return result;
        }
        注销热键:
        public void UnRegKey(IntPtr hwnd, int hotKey_id)
         {
            UnregisterHotKey(hwnd, hotKey_id);
         }
热键编号
VK_ESCAPE = 27;//esc
VK_UP = 38;//↑
VK_DOWN = 40;//↓
VK_LEFT = 37;//←
VK_RIGHT = 39;//→
......
组合键码:
private const int MOD_ALT = 0x1; //ALT
private const int MOD_CONTROL = 0x2; //CTRL
private const int MOD_SHIFT = 0x4; //SHIFT
private const int MOD_WIN = 0x8;//windows
    小结:感觉第二种还不如第一种方法方便.
第二部分:模拟鼠标点击
    导入API:
        [DllImport("user32")]
        private static extern int mouse_event(int dwFlags, int dx, int dy, int cButtons, int dwExtraInfo);
        const int MOUSEEVENTF_MOVE = 0x0001;
        const int MOUSEEVENTF_LEFTDOWN = 0x0002;
        const int MOUSEEVENTF_LEFTUP = 0x0004;
        const int MOUSEEVENTF_RIGHTDOWN = 0x0008;
        const int MOUSEEVENTF_RIGHTUP = 0x0010;
        const int MOUSEEVENTF_MIDDLEDOWN = 0x0020;
        const int MOUSEEVENTF_MIDDLEUP = 0x0040;
        const int MOUSEEVENTF_ABSOLUTE = 0x8000;
    在某个子程序中调用即可(本例当然是放到时钟周期事件中即可)
            mouse_event(MOUSEEVENTF_MOVE, 0, 0, 0, 0); //移动鼠标,
            mouse_event(MOUSEEVENTF_LEFTDOWN, 0, 0, 0, 0);//鼠标左键按下
            mouse_event(MOUSEEVENTF_LEFTUP, 0, 0, 0, 0);//鼠标左键抬起,注意按下,抬起才算一次单击
            //mouse_event(MOUSEEVENTF_RIGHTDOWN, 0, 0, 0, 0);//鼠标右键按下
            //mouse_event(MOUSEEVENTF_RIGHTUP, 0, 0, 0, 0);//鼠标右键抬起
第三部分:模拟键盘
    就没这么复杂了,差不多就象下边这些语句了:
           textBox1.Focus();           //某控件得到焦点
           SendKeys.Send("^a");    //Ctrl+A
           SendKeys.Send("+(ab)");   //Shift+"ab"
           SendKeys.Send("+ab");    //Shift+'a'+'b'

           SendKeys.Send("{DEL}");   //Delete
            SendKeys.Send("{ENTER}");  //Enter
            SendKeys.Send("{a 10}");  //"aaaaaaaaaa"
...其它的慢慢去查.

 

 

posted @ 2012-09-08 17:03  小颗豆  阅读(3132)  评论(0编辑  收藏  举报