better-genshin-impact/BetterGenshinImpact/Model/HotKeySettingModel.cs

199 lines
6.4 KiB
C#
Raw Normal View History

2024-02-03 23:04:14 +08:00
using CommunityToolkit.Mvvm.ComponentModel;
using CommunityToolkit.Mvvm.Input;
2023-11-25 17:04:36 +08:00
using Fischless.HotkeyCapture;
2024-02-03 23:04:14 +08:00
using System;
2024-10-01 01:58:00 +08:00
using System.Collections.ObjectModel;
2024-02-03 23:04:14 +08:00
using System.Diagnostics;
2024-02-06 13:23:36 +08:00
using System.Windows.Forms;
using System.Windows.Input;
2023-10-14 16:10:02 +08:00
namespace BetterGenshinImpact.Model;
/// <summary>
/// 在页面展示快捷键配置的对象
/// </summary>
public partial class HotKeySettingModel : ObservableObject
{
[ObservableProperty] private HotKey _hotKey;
2024-02-03 23:04:14 +08:00
/// <summary>
2024-02-06 18:25:51 +08:00
/// 键鼠监听、全局热键
2024-02-03 23:04:14 +08:00
/// </summary>
[ObservableProperty] private HotKeyTypeEnum _hotKeyType;
2024-02-06 13:23:36 +08:00
[ObservableProperty] private string _hotKeyTypeName;
2024-02-03 23:04:14 +08:00
2024-10-01 01:58:00 +08:00
[ObservableProperty]
private ObservableCollection<HotKeySettingModel> _children = [];
2023-10-14 16:10:02 +08:00
public string FunctionName { get; set; }
2024-10-01 13:50:59 +08:00
public bool IsExpanded => true;
2024-10-01 01:58:00 +08:00
/// <summary>
/// 界面上显示是文件夹而不是快捷键
/// </summary>
[ObservableProperty]
private bool _isDirectory;
2023-10-14 16:10:02 +08:00
public string ConfigPropertyName { get; set; }
2024-04-20 00:14:27 +08:00
public Action<object?, KeyPressedEventArgs>? OnKeyPressAction { get; set; }
public Action<object?, KeyPressedEventArgs>? OnKeyDownAction { get; set; }
public Action<object?, KeyPressedEventArgs>? OnKeyUpAction { get; set; }
2023-10-14 16:10:02 +08:00
2024-02-06 13:23:36 +08:00
public bool IsHold { get; set; }
2024-02-06 18:25:51 +08:00
[ObservableProperty] private bool _switchHotkeyTypeEnabled;
2024-02-03 23:04:14 +08:00
/// <summary>
/// 全局热键配置
/// </summary>
2024-02-06 13:23:36 +08:00
public HotkeyHook? GlobalRegisterHook { get; set; }
/// <summary>
/// 键盘监听配置
/// </summary>
public KeyboardHook? KeyboardMonitorHook { get; set; }
/// <summary>
/// 鼠标监听配置
/// </summary>
public MouseHook? MouseMonitorHook { get; set; }
2024-10-01 01:58:00 +08:00
public HotKeySettingModel(string functionName)
{
FunctionName = functionName;
IsDirectory = true;
}
2024-04-20 00:14:27 +08:00
public HotKeySettingModel(string functionName, string configPropertyName, string hotkey, string hotKeyTypeCode, Action<object?, KeyPressedEventArgs>? onKeyPressAction, bool isHold = false)
2023-10-14 16:10:02 +08:00
{
FunctionName = functionName;
ConfigPropertyName = configPropertyName;
HotKey = HotKey.FromString(hotkey);
2024-02-03 23:04:14 +08:00
HotKeyType = (HotKeyTypeEnum)Enum.Parse(typeof(HotKeyTypeEnum), hotKeyTypeCode);
HotKeyTypeName = HotKeyType.ToChineseName();
2024-04-20 00:14:27 +08:00
OnKeyPressAction = onKeyPressAction;
2024-02-06 13:23:36 +08:00
IsHold = isHold;
2024-02-06 18:25:51 +08:00
SwitchHotkeyTypeEnabled = !isHold;
2023-10-14 16:10:02 +08:00
}
public void RegisterHotKey()
{
if (HotKey.IsEmpty)
{
return;
}
2024-02-06 13:23:36 +08:00
try
2023-10-14 16:10:02 +08:00
{
2024-02-06 13:23:36 +08:00
if (HotKeyType == HotKeyTypeEnum.GlobalRegister)
2024-02-03 23:04:14 +08:00
{
Hotkey hotkey = new(HotKey.ToString());
2024-02-06 13:23:36 +08:00
GlobalRegisterHook?.Dispose();
GlobalRegisterHook = new HotkeyHook();
2024-04-20 00:14:27 +08:00
if (OnKeyPressAction != null)
{
GlobalRegisterHook.KeyPressed -= OnKeyPressed;
GlobalRegisterHook.KeyPressed += OnKeyPressed;
}
2024-02-06 13:23:36 +08:00
GlobalRegisterHook.RegisterHotKey(hotkey.ModifierKey, hotkey.Key);
2024-02-03 23:04:14 +08:00
}
2024-02-06 13:23:36 +08:00
else
2024-02-03 23:04:14 +08:00
{
2024-02-06 13:23:36 +08:00
MouseMonitorHook?.Dispose();
KeyboardMonitorHook?.Dispose();
if (HotKey.MouseButton is MouseButton.XButton1 or MouseButton.XButton2)
{
MouseMonitorHook = new MouseHook
{
IsHold = IsHold
};
2024-04-20 00:14:27 +08:00
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;
}
2024-02-06 13:23:36 +08:00
MouseMonitorHook.RegisterHotKey((MouseButtons)Enum.Parse(typeof(MouseButtons), HotKey.MouseButton.ToString()));
}
else
{
// 如果是组合键,不支持
if (HotKey.Modifiers != ModifierKeys.None)
{
HotKey = HotKey.None;
return;
}
2024-02-06 13:23:36 +08:00
KeyboardMonitorHook = new KeyboardHook
{
IsHold = IsHold
};
2024-04-20 00:14:27 +08:00
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;
}
2024-02-06 13:23:36 +08:00
KeyboardMonitorHook.RegisterHotKey((Keys)Enum.Parse(typeof(Keys), HotKey.Key.ToString()));
}
2024-02-03 23:04:14 +08:00
}
2023-10-14 16:10:02 +08:00
}
2024-02-06 13:23:36 +08:00
catch (Exception e)
2023-10-14 16:10:02 +08:00
{
2024-02-06 13:23:36 +08:00
Debug.WriteLine(e);
HotKey = HotKey.None;
2023-10-14 16:10:02 +08:00
}
}
2024-02-03 23:04:14 +08:00
private void OnKeyPressed(object? sender, KeyPressedEventArgs e)
2023-11-25 17:04:36 +08:00
{
2024-04-20 00:14:27 +08:00
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);
2023-11-25 17:04:36 +08:00
}
2023-10-14 16:10:02 +08:00
public void UnRegisterHotKey()
{
GlobalRegisterHook?.Dispose();
MouseMonitorHook?.Dispose();
KeyboardMonitorHook?.Dispose();
2024-02-03 23:04:14 +08:00
}
[RelayCommand]
public void OnSwitchHotKeyType()
{
HotKeyType = HotKeyType == HotKeyTypeEnum.GlobalRegister ? HotKeyTypeEnum.KeyboardMonitor : HotKeyTypeEnum.GlobalRegister;
HotKeyTypeName = HotKeyType.ToChineseName();
2023-10-14 16:10:02 +08:00
}
2024-04-20 00:14:27 +08:00
}