using CommunityToolkit.Mvvm.ComponentModel; using CommunityToolkit.Mvvm.Input; using Fischless.HotkeyCapture; using System; using System.Diagnostics; using System.Windows.Forms; using System.Windows.Input; namespace BetterGenshinImpact.Model; /// /// 在页面展示快捷键配置的对象 /// public partial class HotKeySettingModel : ObservableObject { [ObservableProperty] private HotKey _hotKey; /// /// 键鼠监听、全局热键 /// [ObservableProperty] private HotKeyTypeEnum _hotKeyType; [ObservableProperty] private string _hotKeyTypeName; public string FunctionName { get; set; } public string ConfigPropertyName { get; set; } public Action OnKeyAction { get; set; } public bool IsHold { get; set; } [ObservableProperty] private bool _switchHotkeyTypeEnabled; /// /// 全局热键配置 /// public HotkeyHook? GlobalRegisterHook { get; set; } /// /// 键盘监听配置 /// public KeyboardHook? KeyboardMonitorHook { get; set; } /// /// 鼠标监听配置 /// public MouseHook? MouseMonitorHook { get; set; } public HotKeySettingModel(string functionName, string configPropertyName, string hotkey, string hotKeyTypeCode, Action onKeyAction, bool isHold = false) { FunctionName = functionName; ConfigPropertyName = configPropertyName; HotKey = HotKey.FromString(hotkey); HotKeyType = (HotKeyTypeEnum)Enum.Parse(typeof(HotKeyTypeEnum), hotKeyTypeCode); HotKeyTypeName = HotKeyType.ToChineseName(); OnKeyAction = onKeyAction; IsHold = isHold; SwitchHotkeyTypeEnabled = !isHold; } public void RegisterHotKey() { if (HotKey.IsEmpty) { return; } try { if (HotKeyType == HotKeyTypeEnum.GlobalRegister) { Hotkey hotkey = new(HotKey.ToString()); GlobalRegisterHook?.Dispose(); GlobalRegisterHook = new HotkeyHook(); GlobalRegisterHook.KeyPressed -= OnKeyPressed; GlobalRegisterHook.KeyPressed += OnKeyPressed; GlobalRegisterHook.RegisterHotKey(hotkey.ModifierKey, hotkey.Key); } else { MouseMonitorHook?.Dispose(); KeyboardMonitorHook?.Dispose(); if (HotKey.MouseButton is MouseButton.XButton1 or MouseButton.XButton2) { MouseMonitorHook = new MouseHook { IsHold = IsHold }; MouseMonitorHook.MousePressed -= OnKeyPressed; MouseMonitorHook.MousePressed += OnKeyPressed; MouseMonitorHook.RegisterHotKey((MouseButtons)Enum.Parse(typeof(MouseButtons), HotKey.MouseButton.ToString())); } else { KeyboardMonitorHook = new KeyboardHook { IsHold = IsHold }; KeyboardMonitorHook.KeyPressed -= OnKeyPressed; KeyboardMonitorHook.KeyPressed += OnKeyPressed; KeyboardMonitorHook.RegisterHotKey((Keys)Enum.Parse(typeof(Keys), HotKey.Key.ToString())); } } } catch (Exception e) { Debug.WriteLine(e); HotKey = HotKey.None; } } private void OnKeyPressed(object? sender, KeyPressedEventArgs e) { OnKeyAction.Invoke(sender, e); } public void UnRegisterHotKey() { if (HotKeyType == HotKeyTypeEnum.GlobalRegister) { GlobalRegisterHook?.Dispose(); } else { MouseMonitorHook?.Dispose(); KeyboardMonitorHook?.Dispose(); } } [RelayCommand] public void OnSwitchHotKeyType() { HotKeyType = HotKeyType == HotKeyTypeEnum.GlobalRegister ? HotKeyTypeEnum.KeyboardMonitor : HotKeyTypeEnum.GlobalRegister; HotKeyTypeName = HotKeyType.ToChineseName(); } }