support key mouse monitor hotkey

This commit is contained in:
huiyadanli
2024-02-06 13:23:36 +08:00
parent 78364c5fa7
commit a4d4aef130
8 changed files with 268 additions and 37 deletions

View File

@@ -5,6 +5,7 @@ using System.Diagnostics;
using System.Windows.Forms;
using BetterGenshinImpact.GameTask;
using BetterGenshinImpact.GameTask.QuickTeleport;
using BetterGenshinImpact.Model;
using Vanara.PInvoke;
namespace BetterGenshinImpact.Core;
@@ -60,7 +61,11 @@ public class MouseKeyMonitor
private void GlobalHookKeyDown(object? sender, KeyEventArgs e)
{
//Debug.WriteLine("KeyDown: \t{0}", e.KeyCode);
Debug.WriteLine("KeyDown: \t{0}", e.KeyCode);
// 热键按下事件
HotKeyDown(sender, e);
if (e.KeyCode == Keys.Space)
{
if (_firstSpaceKeyDownTime == DateTime.MaxValue)
@@ -99,9 +104,15 @@ public class MouseKeyMonitor
}
}
private void GlobalHookKeyUp(object? sender, KeyEventArgs e)
{
//Debug.WriteLine("KeyUp: \t{0}", e.KeyCode);
Debug.WriteLine("KeyUp: \t{0}", e.KeyCode);
// 热键松开事件
HotKeyUp(sender, e);
if (e.KeyCode == Keys.Space)
{
if (_firstSpaceKeyDownTime != DateTime.MaxValue)
@@ -124,6 +135,22 @@ public class MouseKeyMonitor
}
}
private void HotKeyDown(object? sender, KeyEventArgs e)
{
if (KeyboardHook.AllKeyboardHooks.TryGetValue(e.KeyCode, out var hook))
{
hook.KeyDown(sender, e);
}
}
private void HotKeyUp(object? sender, KeyEventArgs e)
{
if (KeyboardHook.AllKeyboardHooks.TryGetValue(e.KeyCode, out var hook))
{
hook.KeyUp(sender, e);
}
}
//private void GlobalHookKeyPress(object? sender, KeyPressEventArgs e)
//{
// Debug.WriteLine("KeyPress: \t{0}", e.KeyChar);
@@ -133,9 +160,12 @@ public class MouseKeyMonitor
{
Debug.WriteLine("MouseDown: {0}; \t Location: {1};\t System Timestamp: {2}", e.Button, e.Location, e.Timestamp);
// uncommenting the following line will suppress the middle mouse button click
if (e.Button == MouseButtons.Left)
if (e.Button != MouseButtons.Left)
{
if (MouseHook.AllMouseHooks.TryGetValue(e.Button, out var hook))
{
hook.MouseDown(sender, e);
}
}
}
@@ -143,6 +173,14 @@ public class MouseKeyMonitor
{
Debug.WriteLine("MouseUp: {0}; \t Location: {1};\t System Timestamp: {2}", e.Button, e.Location, e.Timestamp);
if (e.Button != MouseButtons.Left)
{
if (MouseHook.AllMouseHooks.TryGetValue(e.Button, out var hook))
{
hook.MouseUp(sender, e);
}
}
}
public void Unsubscribe()
@@ -151,7 +189,8 @@ public class MouseKeyMonitor
{
_globalHook.KeyDown -= GlobalHookKeyDown;
_globalHook.KeyUp -= GlobalHookKeyUp;
//_globalHook.MouseDownExt -= GlobalHookMouseDownExt;
_globalHook.MouseDownExt -= GlobalHookMouseDownExt;
_globalHook.MouseUpExt -= GlobalHookMouseUpExt;
//_globalHook.KeyPress -= GlobalHookKeyPress;
_globalHook.Dispose();
}

View File

@@ -1,9 +1,5 @@
using BetterGenshinImpact.Core.Simulator;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Threading;
namespace BetterGenshinImpact.GameTask.Macro
{
@@ -11,7 +7,8 @@ namespace BetterGenshinImpact.GameTask.Macro
{
public static void Done()
{
Simulation.SendInput.Mouse.MoveMouseBy(600, 0);
Simulation.SendInputEx.Mouse.MoveMouseBy(500, 0);
Thread.Sleep(50);
}
}
}

View File

@@ -1,7 +1,6 @@
using System;
using System.Text;
using System.Windows.Input;
using System.Xml.Linq;
namespace BetterGenshinImpact.Model;

View File

@@ -3,6 +3,8 @@ using CommunityToolkit.Mvvm.Input;
using Fischless.HotkeyCapture;
using System;
using System.Diagnostics;
using System.Windows.Forms;
using System.Windows.Input;
namespace BetterGenshinImpact.Model;
@@ -19,8 +21,7 @@ public partial class HotKeySettingModel : ObservableObject
/// </summary>
[ObservableProperty] private HotKeyTypeEnum _hotKeyType;
[ObservableProperty]
private string _hotKeyTypeName;
[ObservableProperty] private string _hotKeyTypeName;
public string FunctionName { get; set; }
@@ -28,12 +29,25 @@ public partial class HotKeySettingModel : ObservableObject
public Action<object?, KeyPressedEventArgs> OnKeyAction { get; set; }
public bool IsHold { get; set; }
/// <summary>
/// 全局热键配置
/// </summary>
public HotkeyHook? KeyBindInfo { get; set; }
public HotkeyHook? GlobalRegisterHook { get; set; }
public HotKeySettingModel(string functionName, string configPropertyName, string hotkey, string hotKeyTypeCode, Action<object?, KeyPressedEventArgs> onKeyAction)
/// <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;
@@ -41,6 +55,7 @@ public partial class HotKeySettingModel : ObservableObject
HotKeyType = (HotKeyTypeEnum)Enum.Parse(typeof(HotKeyTypeEnum), hotKeyTypeCode);
HotKeyTypeName = HotKeyType.ToChineseName();
OnKeyAction = onKeyAction;
IsHold = isHold;
}
public void RegisterHotKey()
@@ -50,29 +65,48 @@ public partial class HotKeySettingModel : ObservableObject
return;
}
if (HotKeyType == HotKeyTypeEnum.GlobalRegister)
try
{
try
if (HotKeyType == HotKeyTypeEnum.GlobalRegister)
{
Hotkey hotkey = new(HotKey.ToString());
KeyBindInfo?.Dispose();
KeyBindInfo = new HotkeyHook();
KeyBindInfo.KeyPressed -= OnKeyPressed;
KeyBindInfo.KeyPressed += OnKeyPressed;
KeyBindInfo.RegisterHotKey(hotkey.ModifierKey, hotkey.Key);
GlobalRegisterHook?.Dispose();
GlobalRegisterHook = new HotkeyHook();
GlobalRegisterHook.KeyPressed -= OnKeyPressed;
GlobalRegisterHook.KeyPressed += OnKeyPressed;
GlobalRegisterHook.RegisterHotKey(hotkey.ModifierKey, hotkey.Key);
}
catch (Exception e)
else
{
Debug.WriteLine(e);
HotKey = HotKey.None;
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()));
}
}
}
else
catch (Exception e)
{
Debug.WriteLine(e);
HotKey = HotKey.None;
}
}
private void OnKeyPressed(object? sender, KeyPressedEventArgs e)
@@ -84,11 +118,12 @@ public partial class HotKeySettingModel : ObservableObject
{
if (HotKeyType == HotKeyTypeEnum.GlobalRegister)
{
KeyBindInfo?.Dispose();
GlobalRegisterHook?.Dispose();
}
else
{
MouseMonitorHook?.Dispose();
KeyboardMonitorHook?.Dispose();
}
}
@@ -98,5 +133,4 @@ public partial class HotKeySettingModel : ObservableObject
HotKeyType = HotKeyType == HotKeyTypeEnum.GlobalRegister ? HotKeyTypeEnum.KeyboardMonitor : HotKeyTypeEnum.GlobalRegister;
HotKeyTypeName = HotKeyType.ToChineseName();
}
}

View File

@@ -0,0 +1,79 @@
using Fischless.HotkeyCapture;
using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using System.Windows.Forms;
using Vanara.PInvoke;
namespace BetterGenshinImpact.Model;
public class KeyboardHook
{
public static Dictionary<Keys, KeyboardHook> AllKeyboardHooks = new();
public event EventHandler<KeyPressedEventArgs>? KeyPressed = null;
public bool IsHold { get; set; }
public Keys BindKey { get; set; } = Keys.None;
public bool IsPressed { get; set; }
public void KeyDown(object? sender, KeyEventArgs e)
{
if (e.KeyCode == BindKey)
{
IsPressed = true;
if (IsHold)
{
Task.Run(() => RunAction(e));
}
else
{
KeyPressed?.Invoke(this, new KeyPressedEventArgs(User32.HotKeyModifiers.MOD_NONE, e.KeyCode));
IsPressed = false;
}
}
}
/// <summary>
/// 长按持续执行
/// </summary>
/// <param name="e"></param>
private void RunAction(KeyEventArgs e)
{
lock (this)
{
while (IsPressed)
{
KeyPressed?.Invoke(this, new KeyPressedEventArgs(User32.HotKeyModifiers.MOD_NONE, e.KeyCode));
}
}
}
public void KeyUp(object? sender, KeyEventArgs e)
{
if (e.KeyCode == BindKey)
{
IsPressed = false;
}
}
public void RegisterHotKey(Keys key)
{
BindKey = key;
AllKeyboardHooks.Add(key, this);
}
public void UnregisterHotKey()
{
IsPressed = false;
IsHold = false;
AllKeyboardHooks.Remove(BindKey);
}
public void Dispose()
{
UnregisterHotKey();
}
}

View File

@@ -0,0 +1,80 @@
using Fischless.HotkeyCapture;
using Gma.System.MouseKeyHook;
using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using System.Windows.Forms;
using Vanara.PInvoke;
namespace BetterGenshinImpact.Model;
public class MouseHook
{
public static Dictionary<MouseButtons, MouseHook> AllMouseHooks = new();
public event EventHandler<KeyPressedEventArgs>? MousePressed = null;
public bool IsHold { get; set; }
public MouseButtons BindMouse { get; set; } = MouseButtons.Left;
public bool IsPressed { get; set; }
public void MouseDown(object? sender, MouseEventExtArgs e)
{
if (e.Button != MouseButtons.Left && e.Button == BindMouse)
{
IsPressed = true;
if (IsHold)
{
Task.Run(() => RunAction(e));
}
else
{
MousePressed?.Invoke(this, new KeyPressedEventArgs(User32.HotKeyModifiers.MOD_NONE, Keys.None));
IsPressed = false;
}
}
}
/// <summary>
/// 长按持续执行
/// </summary>
/// <param name="e"></param>
private void RunAction(MouseEventExtArgs e)
{
lock (this)
{
while (IsPressed)
{
MousePressed?.Invoke(this, new KeyPressedEventArgs(User32.HotKeyModifiers.MOD_NONE, Keys.None));
}
}
}
public void MouseUp(object? sender, MouseEventExtArgs e)
{
if (e.Button != MouseButtons.Left && e.Button == BindMouse)
{
IsPressed = false;
}
}
public void RegisterHotKey(MouseButtons mouseButton)
{
BindMouse = mouseButton;
AllMouseHooks.Add(mouseButton, this);
}
public void UnregisterHotKey()
{
IsPressed = false;
IsHold = false;
AllMouseHooks.Remove(BindMouse);
}
public void Dispose()
{
UnregisterHotKey();
}
}

View File

@@ -160,7 +160,8 @@ public partial class HotKeyPageViewModel : ObservableObject
nameof(Config.HotKeyConfig.TurnAroundHotkey),
Config.HotKeyConfig.TurnAroundHotkey,
Config.HotKeyConfig.TurnAroundHotkeyType,
(_, _) => { TurnAroundMacro.Done(); }
(_, _) => { TurnAroundMacro.Done(); },
true
);
HotKeySettingModels.Add(turnAroundHotKeySettingModel);
@@ -169,7 +170,8 @@ public partial class HotKeyPageViewModel : ObservableObject
nameof(Config.HotKeyConfig.EnhanceArtifactHotkey),
Config.HotKeyConfig.EnhanceArtifactHotkey,
Config.HotKeyConfig.EnhanceArtifactHotkeyType,
(_, _) => { QuickEnhanceArtifactMacro.Done(); }
(_, _) => { QuickEnhanceArtifactMacro.Done(); },
true
);
HotKeySettingModels.Add(enhanceArtifactHotKeySettingModel);
@@ -178,7 +180,8 @@ public partial class HotKeyPageViewModel : ObservableObject
nameof(Config.HotKeyConfig.QuickBuyHotkey),
Config.HotKeyConfig.QuickBuyHotkey,
Config.HotKeyConfig.QuickBuyHotkeyType,
(_, _) => { QuickBuyTask.Done(); }
(_, _) => { QuickBuyTask.Done(); },
true
));

View File

@@ -7,7 +7,7 @@ public class KeyPressedEventArgs : EventArgs
public User32.HotKeyModifiers Modifier { get; }
public Keys Key { get; }
internal KeyPressedEventArgs(User32.HotKeyModifiers modifier, Keys key)
public KeyPressedEventArgs(User32.HotKeyModifiers modifier, Keys key)
{
Modifier = modifier;
Key = key;