Files
better-genshin-impact/BetterGenshinImpact/Model/HotKeySettingModel.cs
2024-02-10 22:34:56 +08:00

139 lines
4.3 KiB
C#

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;
/// <summary>
/// 在页面展示快捷键配置的对象
/// </summary>
public partial class HotKeySettingModel : ObservableObject
{
[ObservableProperty] private HotKey _hotKey;
/// <summary>
/// 键鼠监听、全局热键
/// </summary>
[ObservableProperty] private HotKeyTypeEnum _hotKeyType;
[ObservableProperty] private string _hotKeyTypeName;
public string FunctionName { get; set; }
public string ConfigPropertyName { get; set; }
public Action<object?, KeyPressedEventArgs> OnKeyAction { get; set; }
public bool IsHold { get; set; }
[ObservableProperty] private bool _switchHotkeyTypeEnabled;
/// <summary>
/// 全局热键配置
/// </summary>
public HotkeyHook? GlobalRegisterHook { get; set; }
/// <summary>
/// 键盘监听配置
/// </summary>
public KeyboardHook? KeyboardMonitorHook { get; set; }
/// <summary>
/// 鼠标监听配置
/// </summary>
public MouseHook? MouseMonitorHook { get; set; }
public HotKeySettingModel(string functionName, string configPropertyName, string hotkey, string hotKeyTypeCode, Action<object?, KeyPressedEventArgs> 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
{
// 如果是组合键,不支持
if (HotKey.Modifiers != ModifierKeys.None)
{
HotKey = HotKey.None;
return;
}
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()
{
GlobalRegisterHook?.Dispose();
MouseMonitorHook?.Dispose();
KeyboardMonitorHook?.Dispose();
}
[RelayCommand]
public void OnSwitchHotKeyType()
{
HotKeyType = HotKeyType == HotKeyTypeEnum.GlobalRegister ? HotKeyTypeEnum.KeyboardMonitor : HotKeyTypeEnum.GlobalRegister;
HotKeyTypeName = HotKeyType.ToChineseName();
}
}