C# 全域熱鍵設定 與 表單熱鍵設定

來源:互聯網
上載者:User

標籤:des   style   blog   color   io   ar   使用   strong   sp   

1、 表單熱鍵

 首先要設定主表單KeyPreview為true,可直接在屬性中進行設定,
或者在表單載入中設定: this.KeyPreview = true;
 然後添加表單KeyDown事件,如下:
        private void FrmMain_KeyDown(object sender, KeyEventArgs e)
        {            if (e.Alt && e.Shift && e.Control && e.KeyCode == Keys.S)            {                MessageBox.Show("我按了Control +Shift +Alt +S");            }        }

2、全域熱鍵設定

   定義API函數 》 註冊熱鍵 》 卸載熱鍵

   我這裡定義了AppHotKey類,全部代碼如下:

  public class AppHotKey    {        [DllImport("kernel32.dll")]        public static extern uint GetLastError();        //如果函數執行成功,傳回值不為0。        //如果函數執行失敗,傳回值為0。要得到擴充錯誤資訊,調用GetLastError。        [DllImport("user32.dll", SetLastError = true)]        public static extern bool RegisterHotKey(            IntPtr hWnd,                //要定義熱鍵的視窗的控制代碼            int id,                     //定義熱鍵ID(不能與其它ID重複)                       KeyModifiers fsModifiers,   //標識熱鍵是否在按Alt、Ctrl、Shift、Windows等鍵時才會生效            Keys vk                     //定義熱鍵的內容            );        [DllImport("user32.dll", SetLastError = true)]        public static extern bool UnregisterHotKey(            IntPtr hWnd,                //要取消熱鍵的視窗的控制代碼            int id                      //要取消熱鍵的ID            );        //定義了輔助鍵的名稱(將數字轉變為字元以便於記憶,也可去除此枚舉而直接使用數值)        [Flags()]        public enum KeyModifiers        {            None = 0,            Alt = 1,            Ctrl = 2,            Shift = 4,            WindowsKey = 8        }        /// <summary>        /// 註冊熱鍵        /// </summary>        /// <param name="hwnd">視窗控制代碼</param>        /// <param name="hotKey_id">熱鍵ID</param>        /// <param name="keyModifiers">按鍵組合</param>        /// <param name="key">熱鍵</param>        public static void RegKey(IntPtr hwnd, int hotKey_id, KeyModifiers keyModifiers, Keys key)        {            try            {                if (!RegisterHotKey(hwnd, hotKey_id, keyModifiers, key))                {                    if (Marshal.GetLastWin32Error() == 1409) { MessageBox.Show("熱鍵被佔用 !"); }                    else                    {                        MessageBox.Show("註冊熱鍵失敗!");                    }                }            }            catch (Exception) { }        }        /// <summary>        /// 登出熱鍵        /// </summary>        /// <param name="hwnd">視窗控制代碼</param>        /// <param name="hotKey_id">熱鍵ID</param>        public static void UnRegKey(IntPtr hwnd, int hotKey_id)        {            //登出Id號為hotKey_id的熱鍵設定            UnregisterHotKey(hwnd, hotKey_id);        }    }

重寫表單的WndProc函數,在視窗建立的時候註冊熱鍵,視窗銷毀時銷毀熱鍵,代碼如下:

       private const int WM_HOTKEY = 0x312; //視窗訊息-熱鍵        private const int WM_CREATE = 0x1; //視窗訊息-建立        private const int WM_DESTROY = 0x2; //視窗訊息-銷毀        private const int Space = 0x3572; //熱鍵ID        protected override void WndProc(ref Message m)        {            base.WndProc(ref m);            switch (m.Msg)            {                case WM_HOTKEY: //視窗訊息-熱鍵ID                    switch (m.WParam.ToInt32())                    {                        case Space: //熱鍵ID                            MessageBox.Show("我按了Control +Shift +Alt +S");                            break;                        default:                            break;                    }                    break;                case WM_CREATE: //視窗訊息-建立                    AppHotKey.RegKey(Handle, Space, AppHotKey.KeyModifiers.Ctrl | AppHotKey.KeyModifiers.Shift | AppHotKey.KeyModifiers.Alt, Keys.S);                    break;                case WM_DESTROY: //視窗訊息-銷毀                    AppHotKey.UnRegKey(Handle, Space); //銷毀熱鍵                    break;                default:                    break;            }        }

 

C# 全域熱鍵設定 與 表單熱鍵設定

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.