注册系统热键

方法一(在窗体加载时注册,关闭时注销):
[DllImport("user32.dll", SetLastError = true)]
public static extern bool RegisterHotKey(IntPtr hWnd, int id, uint control, Keys vk);// //注册热键(窗体句柄,热键ID,辅助键,实键)
[DllImport("user32")]
public static extern bool UnregisterHotKey(IntPtr hWnd, int id);  //注消热键(句柄,热键ID)  
//辅助键说明:
//None = 0,
//Alt = 1,
//crtl= 2,    
//Shift = 4,
//Windows = 8
//如果有多个辅助键则,例如 alt+crtl是3  直接相加就可以了
 使用方法
  RegisterHotKey(this.Handle, 888, 2, Keys.S);//注册Ctrl+S
  重写:WndProc
        protected override void WndProc(ref Message m)
        {
            switch (m.Msg)
            {
                case 0x0312:    //这个是window消息定义的注册的热键消息
                    if (m.WParam.ToString().Equals("888"))  //如果是我们注册的那个热键
                        try
                        {
                            MessageBox.Show("你已经按了Ctrl+S");
                        }
                        catch (Exception e)
                        {
                            MessageBox.Show(e.ToString());
                        }
                    break;
            }
            base.WndProc(ref m);
        }
  取消注册:UnregisterHotKey(this.Handle, 888);
        //写好的热键类
        public class WinHotKey
        {
            public WinHotKey()
            {
                //
                // TODO:在此处添加构造函数逻辑
                //
            }
            [DllImport("user32.dll", SetLastError = true)]
            public static extern bool RegisterHotKey(IntPtr hWnd, int id, KeyModifiers fsModifiers, Keys vk);
            [DllImport("user32.dll", SetLastError = true)]
            public static extern bool UnregisterHotKey(IntPtr hWnd, int id);
            [Flags()]
            public enum KeyModifiers
            {
                None = 0,
                Alt = 1,
                Control = 2,
                Shift = 4,
                Windows = 8
            }
        }
------------------------------------------------------------------------------------------------------------------------
方法二:
 [DllImport("user32.dll")]
 private static extern int SendMessage(IntPtr hwnd, int wMsg, int wParam, int lParam);
 示例(按Shift+A显示窗体):SendMessage(this.Handle, 0x32,0x141,0);
原文地址:https://www.cnblogs.com/linmilove/p/1500885.html