public HotKeyModel(HotKeyIdentity hotKeyIdentity) { HotKeyIdentity = hotKeyIdentity; //构造时尝试注册 HotkeyManager.Current.AddOrReplace(HotKeyIdentity.RegisterKey, HotKeyIdentity.Key, HotKeyIdentity.ModifierKeys, OnHotkeyEventHandler); }
/// <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); }