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? OnKeyPressAction { get; set; }
public Action? OnKeyDownAction { get; set; }
public Action? OnKeyUpAction { 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? onKeyPressAction, bool isHold = false)
{
FunctionName = functionName;
ConfigPropertyName = configPropertyName;
HotKey = HotKey.FromString(hotkey);
HotKeyType = (HotKeyTypeEnum)Enum.Parse(typeof(HotKeyTypeEnum), hotKeyTypeCode);
HotKeyTypeName = HotKeyType.ToChineseName();
OnKeyPressAction = onKeyPressAction;
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();
if (OnKeyPressAction != null)
{
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
};
if (OnKeyPressAction != null)
{
MouseMonitorHook.MousePressed -= OnKeyPressed;
MouseMonitorHook.MousePressed += OnKeyPressed;
}
if (OnKeyDownAction != null)
{
MouseMonitorHook.MouseDownEvent -= OnKeyDown;
MouseMonitorHook.MouseDownEvent += OnKeyDown;
}
if (OnKeyUpAction != null)
{
MouseMonitorHook.MouseUpEvent -= OnKeyUp;
MouseMonitorHook.MouseUpEvent += OnKeyUp;
}
MouseMonitorHook.RegisterHotKey((MouseButtons)Enum.Parse(typeof(MouseButtons), HotKey.MouseButton.ToString()));
}
else
{
// 如果是组合键,不支持
if (HotKey.Modifiers != ModifierKeys.None)
{
HotKey = HotKey.None;
return;
}
KeyboardMonitorHook = new KeyboardHook
{
IsHold = IsHold
};
if (OnKeyPressAction != null)
{
KeyboardMonitorHook.KeyPressedEvent -= OnKeyPressed;
KeyboardMonitorHook.KeyPressedEvent += OnKeyPressed;
}
if (OnKeyDownAction != null)
{
KeyboardMonitorHook.KeyDownEvent -= OnKeyDown;
KeyboardMonitorHook.KeyDownEvent += OnKeyDown;
}
if (OnKeyUpAction != null)
{
KeyboardMonitorHook.KeyUpEvent -= OnKeyUp;
KeyboardMonitorHook.KeyUpEvent += OnKeyUp;
}
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)
{
OnKeyPressAction?.Invoke(sender, e);
}
private void OnKeyDown(object? sender, KeyPressedEventArgs e)
{
OnKeyDownAction?.Invoke(sender, e);
}
private void OnKeyUp(object? sender, KeyPressedEventArgs e)
{
OnKeyUpAction?.Invoke(sender, e);
}
public void UnRegisterHotKey()
{
GlobalRegisterHook?.Dispose();
MouseMonitorHook?.Dispose();
KeyboardMonitorHook?.Dispose();
}
[RelayCommand]
public void OnSwitchHotKeyType()
{
HotKeyType = HotKeyType == HotKeyTypeEnum.GlobalRegister ? HotKeyTypeEnum.KeyboardMonitor : HotKeyTypeEnum.GlobalRegister;
HotKeyTypeName = HotKeyType.ToChineseName();
}
}