給你的程式增加熱鍵(C#)

來源:互聯網
上載者:User

Form1.cs

            private void Form1_Load(object sender, System.EventArgs e)
            {
                  // 設定熱鍵
                  SetHotKey(false, false, false,false,Keys.Space );
            }
           
            private bool key_Ctrl = false;
            private bool key_Shift = false;
            private bool key_Alt = false;
            private bool key_Windows = false;
            private Keys   key_other;

            public void SetHotKey(bool bCtrl,bool bShift,bool bAlt,bool  bWindows,Keys nowKey)
            {
                  try
                  {
                        this.key_Alt = bAlt;
                        this.key_Ctrl = bCtrl;
                        this.key_Shift = bShift;
                        this.key_Windows = bWindows;
                        this.key_other = nowKey;
           
                        WinHotKey.KeyModifiers modifier = WinHotKey.KeyModifiers.None;
           
                        if( this.key_Ctrl )
                              modifier |= WinHotKey.KeyModifiers.Control;
                        if(this.key_Alt )
                              modifier |= WinHotKey.KeyModifiers.Alt;
                        if(this.key_Shift)
                              modifier |= WinHotKey.KeyModifiers.Shift;
                        if(this.key_Windows)
                              modifier |= WinHotKey.KeyModifiers.Windows;
           
                        WinHotKey.RegisterHotKey(Handle,100,modifier,nowKey);
                  }
                  catch
                  {
                        MessageBox.Show ("快速鍵定義錯誤!");
                  }
            }
           
            protected override void WndProc(ref Message msg )
            {
                  const int WM_HOTKEY =  0x0312; // 熱鍵訊息

                  if (msg.Msg != WM_HOTKEY)
                  {
                        base.WndProc(ref msg);
                  }
                  else
                  {
                        MessageBox.Show("Hotkey pressed"); //啟用熱鍵
                        // ProcessHotKey();
                  }
            }

            private void Form1_Closing(object sender, System.ComponentModel.CancelEventArgs e)
            {
                  WinHotKey.UnregisterHotKey(Handle, 100);      // 登出熱鍵
            }

WinHotKey.cs

      public class WinHotKey
      {
            public WinHotKey()
            {
           
            }

            [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
            }

      }

完整源碼下載

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.