c#全局热键

 
private void load(object sender, RoutedEventArgs e)
        {
            //System.Windows.MessageBox.Show("L:l");.
            IntPtr Handle = new WindowInteropHelper(this).Handle;
            //注册热键Shift+h,Id号为103。
            HotKey.RegisterHotKey(Handle, 103, HotKey.KeyModifiers.Ctrl| HotKey.KeyModifiers.Alt, System.Windows.Forms.Keys.H);
           //注册热键Shift+M,Id号为104。 
            HotKey.RegisterHotKey(Handle, 104, HotKey.KeyModifiers.Ctrl| HotKey.KeyModifiers.Alt, System.Windows.Forms.Keys.M);
            RegisterHotKey();
  
          
        }
        private void RegisterHotKey()
        {
            IntPtr hwnd = new WindowInteropHelper(this).Handle;
            //获得消息源
            System.Windows.Interop.HwndSource source = System.Windows.Interop.HwndSource.FromHwnd(hwnd);


            source.AddHook(HotKeyHook);
        }


        private IntPtr HotKeyHook(IntPtr hwnd, int msg, IntPtr wParam, IntPtr lParam, ref bool handled)
        //热键处理过程
        {
            const int WM_HOTKEY = 0x0312;//如果m.Msg的值为0x0312那么表示用户按下了热键
            if (msg == WM_HOTKEY)
            {
                switch (wParam.ToInt32())
                {
                    case 103:
                        //此处填写快捷键响应代码
                        break;
                    case 104:
                        //ctrl+shift+m响应事件
                        break;

                }
            }
            return IntPtr.Zero;
        }

HotKey.cs
    
using System;
using System.Runtime.InteropServices;

namespace enter
{
    class HotKey
    {
        //如果函数执行成功,返回值不为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等键时才会生效  
            System.Windows.Forms.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
        }
    }
}

这富文本编辑器好难用。。。。上面的代码块去不掉了。。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值