示例#1
0
 public static bool Regist(HotKey key, HotKeyCallBackHandler callBack)
 {
     if (key == null)
     {
         throw new ArgumentNullException(nameof(key));
     }
     if (callBack == null)
     {
         throw new ArgumentNullException(nameof(callBack));
     }
     try
     {
         _hotKeyManager.Register(key);
         _keymap[key] = callBack;
         return(true);
     }
     catch (ArgumentException)
     {
         return(true);
     }
     catch (Win32Exception)
     {
         return(false);
     }
 }
 public static bool Regist(HotKey key, HotKeyCallBackHandler callBack)
 {
     if (key == null)
     {
         throw new ArgumentNullException(nameof(key));
     }
     if (callBack == null)
     {
         throw new ArgumentNullException(nameof(callBack));
     }
     try
     {
         _hotKeyManager.Register(key);
         _keymap[key] = callBack;
         return(true);
     }
     catch (ArgumentException)
     {
         // already called this method with the specific hotkey
         // return success silently
         return(true);
     }
     catch (Win32Exception)
     {
         // this hotkey already registered by other programs
         // notify user to change key
         return(false);
     }
 }
 public static bool Regist(Key key, ModifierKeys modifiers, HotKeyCallBackHandler callBack)
 {
     if (!Enum.IsDefined(typeof(Key), key))
     {
         throw new InvalidEnumArgumentException(nameof(key), (int)key, typeof(Key));
     }
     try
     {
         var hotkey = _hotKeyManager.Register(key, modifiers);
         _keymap[hotkey] = callBack;
         return(true);
     }
     catch (ArgumentException)
     {
         // already called this method with the specific hotkey
         // return success silently
         return(true);
     }
     catch (Win32Exception)
     {
         // already registered by other programs
         // notify user to change key
         return(false);
     }
 }
示例#4
0
        //注册快捷键
        public bool RegisterGlobalHotKey(IntPtr hWnd, int modifiers, Keys vk, HotKeyCallBackHandler callBack)
        {
            int  id = _keyId++;
            bool registerHotkeyResult = RegisterHotKey(hWnd, id, modifiers, vk);

            _keymap[id] = callBack;
            return(registerHotkeyResult);
        }
示例#5
0
 public void Unregist(HotKeyCallBackHandler callBack)
 {
     lock (_locker)
     {
         foreach (var pair in _dictHotKeyId2CallBack.Where(pair => pair.Value == callBack).ToArray())
         {
             Unregist(pair.Key);
         }
     }
 }
示例#6
0
 // 注销快捷键
 public void UnRegisterGlobalHotKey(IntPtr hWnd, HotKeyCallBackHandler callBack)
 {
     foreach (KeyValuePair <int, HotKeyCallBackHandler> var in _keymap)
     {
         if (var.Value == callBack)
         {
             UnregisterHotKey(hWnd, var.Key);
             return;
         }
     }
 }
示例#7
0
 public static bool IsCallbackExists( HotKeyCallBackHandler cb, out HotKey hotkey)
 {
     if (cb == null) throw new ArgumentNullException(nameof(cb));
     try
     {
         var key = _keymap.First(x => x.Value == cb).Key;
         hotkey = key;
         return true;
     }
     catch (InvalidOperationException)
     {
         // not found
         hotkey = null;
         return false;
     }
 }
示例#8
0
 public static bool IsCallbackExists(HotKeyCallBackHandler cb, out HotKey hotkey)
 {
     if (cb == null)
     {
         throw new ArgumentNullException(nameof(cb));
     }
     if (_keymap.Any(v => v.Value == cb))
     {
         hotkey = _keymap.First(v => v.Value == cb).Key;
         return(true);
     }
     else
     {
         hotkey = null;
         return(false);
     }
 }
示例#9
0
 public static bool IsCallbackExists(HotKeyCallBackHandler cb, out HotKey hotkey)
 {
     if (cb == null)
     {
         throw new ArgumentNullException(nameof(cb));
     }
     try
     {
         var key = _keymap.First(x => x.Value == cb).Key;
         hotkey = key;
         return(true);
     }
     catch (InvalidOperationException)
     {
         hotkey = null;
         return(false);
     }
 }
示例#10
0
        /// <summary>
        /// 根据键值组合字符串注册
        /// </summary>
        /// <param name="str"></param>
        /// <param name="handle"></param>
        /// <param name="callback"></param>
        public bool RegisterHotKeyByStr(string str, IntPtr handle, HotKeyCallBackHandler callback)
        {
            if (str == "")
            {
                return(false);
            }
            int  modifiers = 0;
            Keys vk        = Keys.None;

            foreach (string value in str.Split('+'))
            {
                switch (value.Trim())
                {
                case "Ctrl":
                    modifiers += (int)HotkeyModifiers.Control;
                    break;

                case "Alt":
                    modifiers += (int)HotkeyModifiers.Alt;
                    break;

                case "Shift":
                    modifiers += (int)HotkeyModifiers.Shift;
                    break;

                default:
                {
                    if (Regex.IsMatch(value, @"[0-9]"))
                    {
                        vk = (Keys)Enum.Parse(typeof(Keys), "D" + value.Trim());
                    }
                    else
                    {
                        vk = (Keys)Enum.Parse(typeof(Keys), value.Trim());
                    }
                    break;
                }
                }
            }


            //这里注册了Ctrl+Alt+E 快捷键
            return(RegisterGlobalHotKey(handle, modifiers, vk, callback));
        }
示例#11
0
 public static bool Regist(Key key, ModifierKeys modifiers, HotKeyCallBackHandler callBack)
 {
     if (!Enum.IsDefined(typeof(Key), key))
     {
         throw new InvalidEnumArgumentException(nameof(key), (int)key, typeof(Key));
     }
     try
     {
         var hotkey = _hotKeyManager.Register(key, modifiers);
         _keymap[hotkey] = callBack;
         return(true);
     }
     catch (ArgumentException)
     {
         return(true);
     }
     catch (Win32Exception)
     {
         return(false);
     }
 }
示例#12
0
        /// <summary>
        /// Regist a hotkey, it will return: status code + hot key in string + hot key id
        /// </summary>
        /// <param name="window">wpf window</param>
        /// <param name="hotkeyModifiers"></param>
        /// <param name="key"></param>
        /// <param name="callBack"></param>
        public Tuple <RetCode, string, int> Regist(Window window, uint hotkeyModifiers, System.Windows.Input.Key key, HotKeyCallBackHandler callBack)
        {
            lock (_locker)
            {
                var hotKeyString = GetHotKeyString(hotkeyModifiers, key);
                var hWnd         = IntPtr.Zero;
                if (window != null)
                {
                    var win = new System.Windows.Interop.WindowInteropHelper(window);
                    hWnd = win.Handle;
                    if (!_hookedhWnd.Contains(hWnd) && hWnd != IntPtr.Zero)
                    {
                        var source = System.Windows.Interop.HwndSource.FromHwnd(hWnd);
                        source.AddHook(HookHandel);
                    }
                }

                while (_dictHotKeyId2hWnd.ContainsKey(_hotKeyId))
                {
                    ++_hotKeyId;
                }

                // ref https://docs.microsoft.com/en-us/windows/win32/api/winuser/nf-winuser-registerhotkey
                var vk = System.Windows.Input.KeyInterop.VirtualKeyFromKey(key);
                if (!RegisterHotKey(hWnd, _hotKeyId, hotkeyModifiers, (uint)vk))    // If the function succeeds, the return value is nonzero.
                {
                    var errorCode = GetLastError();
                    var code      = errorCode == (int)RetCode.ERROR_HOTKEY_ALREADY_REGISTERED ? RetCode.ERROR_HOTKEY_ALREADY_REGISTERED : RetCode.ERROR_HOTKEY_NOT_REGISTERED;
                    return(new Tuple <RetCode, string, int>(code, hotKeyString, 0));
                }

                _hookedhWnd.Add(hWnd);
                _dictHotKeyId2hWnd[_hotKeyId]     = hWnd;
                _dictHotKeyId2CallBack[_hotKeyId] = callBack;

                return(new Tuple <RetCode, string, int>(RetCode.Success, hotKeyString, _hotKeyId));
            }
        }
示例#13
0
 public static bool RegHotkey(HotKey hotkey, HotKeyCallBackHandler callback)
 {
     UnregExistingHotkey(callback);
     return(Register(hotkey, callback));
 }
示例#14
0
 public static bool Regist( HotKey key, HotKeyCallBackHandler callBack )
 {
     if (key == null)
         throw new ArgumentNullException(nameof(key));
     if (callBack == null)
         throw new ArgumentNullException(nameof(callBack));
     try
     {
         _hotKeyManager.Register(key);
         _keymap[key] = callBack;
         return true;
     }
     catch (ArgumentException)
     {
         // already called this method with the specific hotkey
         // return success silently
         return true;
     }
     catch (Win32Exception)
     {
         // this hotkey already registered by other programs
         // notify user to change key
         return false;
     }
 }
示例#15
0
 public static bool Regist(Key key, ModifierKeys modifiers, HotKeyCallBackHandler callBack)
 {
     if (!Enum.IsDefined(typeof(Key), key))
         throw new InvalidEnumArgumentException(nameof(key), (int) key, typeof(Key));
     try
     {
         var hotkey = _hotKeyManager.Register(key, modifiers);
         _keymap[hotkey] = callBack;
         return true;
     }
     catch (ArgumentException)
     {
         // already called this method with the specific hotkey
         // return success silently
         return true;
     }
     catch (Win32Exception)
     {
         // already registered by other programs
         // notify user to change key
         return false;
     }
 }
示例#16
0
 /// <summary>
 /// Regist a hotkey, it will return: status code + hot key in string + hot key id
 /// </summary>
 public Tuple <RetCode, string, int> Regist(Window window, HotkeyModifiers hotkeyModifiers, System.Windows.Input.Key key, HotKeyCallBackHandler callBack)
 {
     return(Regist(window, (uint)hotkeyModifiers, key, callBack));
 }