给你的程序增加热键(C#)

本文介绍了一个使用C#实现的热键注册与监听功能的简单示例。该示例通过自定义类WinHotKey实现了热键的注册与注销,并在主窗体Form1中设置了Space键为热键。当按下设定的热键时,会弹出提示框。

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
            }

      }

完整源码下载

转载于:https://www.cnblogs.com/Truly/archive/2006/04/14/375043.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值