diff --git a/Core/ViewModel/Pages/SettingPageViewModel.cs b/Core/ViewModel/Pages/SettingPageViewModel.cs index 8d30907..4dd2050 100644 --- a/Core/ViewModel/Pages/SettingPageViewModel.cs +++ b/Core/ViewModel/Pages/SettingPageViewModel.cs @@ -7,6 +7,7 @@ using System.Windows; using CommunityToolkit.Mvvm.ComponentModel; using CommunityToolkit.Mvvm.Input; +using Core.SDKs.HotKey; using Core.SDKs.Services; using Core.SDKs.Services.Config; using log4net; @@ -97,8 +98,29 @@ private void EditHotKey(string name) } var xx = (Window)hwndSource.RootVisual; + var hotKeyModel = ConfigManger.Config.hotKeys.FirstOrDefault(e => + { + if ($"{e.MainName}_{e.Name}".Equals(name)) + { + return true; + } - ((IHotKeyEditor)ServiceManager.Services.GetService(typeof(IHotKeyEditor))!).EditByName(name?.ToString(), xx); + return false; + }); + if (hotKeyModel is null) + { + var strings = name.Split("_", 2); + var hotKeyModel2 = new HotKeyModel() + { MainName = strings[0], Name = strings[1], IsUsable = true }; + ConfigManger.Config.hotKeys.Add(hotKeyModel2); + ConfigManger.Save(); + ((IHotKeyEditor)ServiceManager.Services.GetService(typeof(IHotKeyEditor))!).EditByName(name, xx); + } + else + { + ((IHotKeyEditor)ServiceManager.Services.GetService(typeof(IHotKeyEditor))!).EditByHotKeyModel(hotKeyModel, + xx); + } } [RelayCommand] diff --git a/uToolkitopia/Controls/HotKeyShow.cs b/uToolkitopia/Controls/HotKeyShow.cs index 93a988e..5426f62 100644 --- a/uToolkitopia/Controls/HotKeyShow.cs +++ b/uToolkitopia/Controls/HotKeyShow.cs @@ -4,9 +4,13 @@ using System.Linq; using System.Windows; using System.Windows.Controls.Primitives; +using System.Windows.Input; +using CommunityToolkit.Mvvm.Input; using CommunityToolkit.Mvvm.Messaging; using Core.SDKs.HotKey; +using Core.SDKs.Services; using Core.SDKs.Services.Config; +using Kitopia.View; #endregion @@ -56,15 +60,20 @@ public enum KeyTypeE typeof(string), typeof(HotKeyShow), new PropertyMetadata("空格")); + public static readonly DependencyProperty RemoveHotKeyProperty = DependencyProperty.Register(nameof(RemoveHotKey), + typeof(ICommand), typeof(HotKeyShow), new FrameworkPropertyMetadata(null)); + public HotKeyShow() { WeakReferenceMessenger.Default.Register(this, "hotkey", (_, s) => { - if (s == HotKeyModel.SignName) + HotKeyModel ??= ConfigManger.Config.hotKeys.FirstOrDefault(e => e.SignName == s); + if (HotKeyModel != null && s == HotKeyModel.SignName) { HotKeyModelChanged(HotKeyModel, this); } }); + SetValue(RemoveHotKeyProperty, new RelayCommand(Remove)); } [Bindable(true)] @@ -99,6 +108,14 @@ public string KeyName private set => SetValue(KeyNameProperty, value); } + [Bindable(true)] + [Category("RemoveHotKey")] + public ICommand RemoveHotKey + { + get => (ICommand)GetValue(RemoveHotKeyProperty); + private set => SetValue(RemoveHotKeyProperty, value); + } + private static void HotKeyModelChanged(HotKeyModel hotKeyModel, HotKeyShow hotKeyShow) { var type = 0000; @@ -130,4 +147,14 @@ private static void HotKeyModelChanged(HotKeyModel hotKeyModel, HotKeyShow hotKe hotKeyShow.KeyType = (HotKeyShow.KeyTypeE)type; hotKeyShow.KeyName = hotKeyModel.SelectKey.ToString(); } + + private void Remove(HotKeyModel? hotKeyModel) + { + if (hotKeyModel != null) + { + ((MainWindow)ServiceManager.Services.GetService(typeof(MainWindow))!).RemoveHotKey(hotKeyModel); + SetValue(HotKeyModelProperty, null); + SetValue(KeyTypeProperty, KeyTypeE.None); + } + } } \ No newline at end of file diff --git a/uToolkitopia/Controls/HotKeyShow.xaml b/uToolkitopia/Controls/HotKeyShow.xaml index 17b60ed..335ffae 100644 --- a/uToolkitopia/Controls/HotKeyShow.xaml +++ b/uToolkitopia/Controls/HotKeyShow.xaml @@ -60,8 +60,13 @@ Height="40" /> + Content="{TemplateBinding KeyName}" Margin="5,0,0,0" Height="40" /> + + + + + + diff --git a/uToolkitopia/SDKs/HotKeyHelper.cs b/uToolkitopia/SDKs/HotKeyHelper.cs index 5256e1b..f6b5caa 100644 --- a/uToolkitopia/SDKs/HotKeyHelper.cs +++ b/uToolkitopia/SDKs/HotKeyHelper.cs @@ -3,6 +3,7 @@ using System; using System.Collections.Generic; using Core.SDKs.HotKey; +using Core.SDKs.Services.Config; using log4net; using Vanara.PInvoke; @@ -59,7 +60,7 @@ public static List RegisterGlobalHotKey(IEnumerable ho private static bool RegisterHotKey(HotKeyModel hotKeyModel, IntPtr hWnd) { var fsModifierKey = new User32.HotKeyModifiers(); - var hotKeySetting = $"{hotKeyModel.MainName}_{hotKeyModel.Name}"; + var hotKeySetting = hotKeyModel.SignName; if (!hotKeyModel.IsUsable) { return true; @@ -108,4 +109,18 @@ private static bool RegisterHotKey(HotKeyModel hotKeyModel, IntPtr hWnd) return User32.RegisterHotKey(hWnd, m_HotKeySettingsDic[hotKeySetting], fsModifierKey, (uint)hotKeyModel.SelectKey); } + + public static void UnRegisterHotKey(HotKeyModel hotKeyModel, IntPtr hwnd) + { + log.Debug("注销热键:" + hotKeyModel.SignName); + User32.UnregisterHotKey(hwnd, m_HotKeySettingsDic[hotKeyModel.SignName]); + if (!Kernel32.GlobalFindAtom(hotKeyModel.SignName).IsInvalid) + { + Kernel32.GlobalDeleteAtom(Kernel32.GlobalFindAtom(hotKeyModel.SignName)); + } + + m_HotKeySettingsDic.Remove(hotKeyModel.SignName); + ConfigManger.Config.hotKeys.RemoveAll(e => e.SignName == hotKeyModel.SignName); + ConfigManger.Save(); + } } \ No newline at end of file diff --git a/uToolkitopia/View/MainWindow.xaml.cs b/uToolkitopia/View/MainWindow.xaml.cs index 9e93527..943124e 100644 --- a/uToolkitopia/View/MainWindow.xaml.cs +++ b/uToolkitopia/View/MainWindow.xaml.cs @@ -162,6 +162,11 @@ public bool HotKeySet(HotKeyModel hotKeyModel) return false; } + public void RemoveHotKey(HotKeyModel hotKeyModel) + { + HotKeyHelper.UnRegisterHotKey(hotKeyModel, m_Hwnd); + } + public void InitHotKey() { var list = ConfigManger.Config.hotKeys;