Ejemplo n.º 1
0
 internal HotKeyHelper()
 {
     HotkeyManager.HotkeyAlreadyRegistered += (sender, args) =>
     {
         WindowsApi.WriteLog($"Hot key {args.Name} already register");
         HotkeyAlreadyRegistered?.Invoke(sender, args);
     };
 }
Ejemplo n.º 2
0
        /// <summary>
        /// 注册或替换快捷键
        /// </summary>
        /// <param name="identity"></param>
        /// <param name="key"></param>
        /// <param name="modifierKeys"></param>
        /// <param name="action"></param>
        /// <returns></returns>
        public bool RegisterOrReplace(string identity, Key key, ModifierKeys modifierKeys, Action action)
        {
            lock (_lock)
            {
                try
                {
                    Remove(identity);

                    var hotKeyIdentity = new HotKeyIdentity(key, modifierKeys);

                    if (_hotKeyCache.ContainsKey(hotKeyIdentity))
                    {
                        _hotKeyCache[hotKeyIdentity].LinkedEventHandler(action, identity);
                    }
                    else
                    {
                        var hotKeyModel = new HotKeyModel(hotKeyIdentity);
                        hotKeyModel.RemoveAllLinkedEvent += HotKeyModelOnRemoveAllLinkedEvent;
                        hotKeyModel.LinkedEventHandler(action, identity);

                        _hotKeyCache.Add(hotKeyIdentity, hotKeyModel);
                    }

                    if (!string.IsNullOrEmpty(identity))
                    {
                        _identityCache.Add(identity, hotKeyIdentity);
                    }

                    return(true);
                }
                catch (HotkeyAlreadyRegisteredException hotkeyAlreadyRegisteredException)
                {
                    WindowsApi.WriteLog($"Hot key {hotkeyAlreadyRegisteredException.Name} already register");
                    HotkeyAlreadyRegistered?.Invoke(HotkeyManager.Current,
                                                    new HotkeyAlreadyRegisteredEventArgs(hotkeyAlreadyRegisteredException.Name));
                }
            }

            return(false);
        }