Skip to content

Commit

Permalink
use new hotkey module
Browse files Browse the repository at this point in the history
  • Loading branch information
huiyadanli committed Nov 25, 2023
1 parent 9fe2a75 commit cef6f6a
Show file tree
Hide file tree
Showing 16 changed files with 643 additions and 17 deletions.
12 changes: 12 additions & 0 deletions BetterGenshinImpact.sln
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,10 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Fischless.GameCapture", "Fi
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Vision.WindowCapture.Test", "Vision.WindowCapture.Test\Vision.WindowCapture.Test.csproj", "{D35CB953-C666-4E57-9A9A-3AAE5BF78402}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Fischless.HotkeyCapture", "Fischless.HotkeyCapture\Fischless.HotkeyCapture.csproj", "{08152E44-2564-46C5-B5B2-54DD43C01A79}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Fischless.KeyboardCapture", "Fischless.KeyboardCapture\Fischless.KeyboardCapture.csproj", "{10A48327-7E58-4B51-B1FC-55506C703C8F}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|x64 = Debug|x64
Expand All @@ -39,6 +43,14 @@ Global
{D35CB953-C666-4E57-9A9A-3AAE5BF78402}.Debug|x64.Build.0 = Debug|x64
{D35CB953-C666-4E57-9A9A-3AAE5BF78402}.Release|x64.ActiveCfg = Release|x64
{D35CB953-C666-4E57-9A9A-3AAE5BF78402}.Release|x64.Build.0 = Release|x64
{08152E44-2564-46C5-B5B2-54DD43C01A79}.Debug|x64.ActiveCfg = Debug|x64
{08152E44-2564-46C5-B5B2-54DD43C01A79}.Debug|x64.Build.0 = Debug|x64
{08152E44-2564-46C5-B5B2-54DD43C01A79}.Release|x64.ActiveCfg = Release|x64
{08152E44-2564-46C5-B5B2-54DD43C01A79}.Release|x64.Build.0 = Release|x64
{10A48327-7E58-4B51-B1FC-55506C703C8F}.Debug|x64.ActiveCfg = Debug|x64
{10A48327-7E58-4B51-B1FC-55506C703C8F}.Debug|x64.Build.0 = Debug|x64
{10A48327-7E58-4B51-B1FC-55506C703C8F}.Release|x64.ActiveCfg = Release|x64
{10A48327-7E58-4B51-B1FC-55506C703C8F}.Release|x64.Build.0 = Release|x64
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand Down
2 changes: 1 addition & 1 deletion BetterGenshinImpact/BetterGenshinImpact.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@

<ItemGroup>
<PackageReference Include="CommunityToolkit.Mvvm" Version="8.2.2" />
<PackageReference Include="GlobalHotkeys" Version="1.0.0.6" />
<PackageReference Include="H.InputSimulator" Version="1.4.1" />
<PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="7.0.0" />
<PackageReference Include="Microsoft.Extensions.Hosting" Version="7.0.1" />
Expand All @@ -52,6 +51,7 @@

<ItemGroup>
<ProjectReference Include="..\Fischless.GameCapture\Fischless.GameCapture.csproj" />
<ProjectReference Include="..\Fischless.HotkeyCapture\Fischless.HotkeyCapture.csproj" />
</ItemGroup>

<ItemGroup>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ public static void Done()
return;
}

SystemControl.ActivateWindow(TaskContext.Instance().GameHandle);
// SystemControl.ActivateWindow(TaskContext.Instance().GameHandle);

var captureArea = TaskContext.Instance().SystemInfo.CaptureAreaRect;
var assetScale = TaskContext.Instance().SystemInfo.AssetScale;
Expand Down
27 changes: 18 additions & 9 deletions BetterGenshinImpact/Model/HotKeySettingModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
using System.Windows;
using BetterGenshinImpact.Helpers;
using CommunityToolkit.Mvvm.ComponentModel;
using Fischless.HotkeyCapture;
using Gma.System.MouseKeyHook.HotKeys;

namespace BetterGenshinImpact.Model;

Expand All @@ -18,11 +20,11 @@ public partial class HotKeySettingModel : ObservableObject

public string ConfigPropertyName { get; set; }

public Action<mrousavy.HotKey> OnKeyAction { get; set; }
public Action<object?, KeyPressedEventArgs> OnKeyAction { get; set; }

public mrousavy.HotKey? KeyBindInfo { get; set; }
public HotkeyHook? KeyBindInfo { get; set; }

public HotKeySettingModel(string functionName, string configPropertyName, string hotkey, Action<mrousavy.HotKey> onKeyAction)
public HotKeySettingModel(string functionName, string configPropertyName, string hotkey, Action<object?, KeyPressedEventArgs> onKeyAction)
{
FunctionName = functionName;
ConfigPropertyName = configPropertyName;
Expand All @@ -39,12 +41,14 @@ public void RegisterHotKey()

try
{
KeyBindInfo = new mrousavy.HotKey(
HotKey.Modifiers,
HotKey.Key,
UIDispatcherHelper.MainWindow,
OnKeyAction
);
Fischless.HotkeyCapture.Hotkey hotkey = new(HotKey.ToString());

KeyBindInfo?.Dispose();
KeyBindInfo = new HotkeyHook();
KeyBindInfo.KeyPressed -= OnKeyPressed;
KeyBindInfo.KeyPressed += OnKeyPressed;
KeyBindInfo.RegisterHotKey(hotkey.ModifierKey, hotkey.Key);

}
catch (Exception e)
{
Expand All @@ -54,6 +58,11 @@ public void RegisterHotKey()

}

private void OnKeyPressed(object? sender, KeyPressedEventArgs e)
{
OnKeyAction.Invoke(sender, e);
}

public void UnRegisterHotKey()
{
KeyBindInfo?.Dispose();
Expand Down
12 changes: 6 additions & 6 deletions BetterGenshinImpact/ViewModel/Pages/HotKeyPageViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ private void BuildHotKeySettingModelList()
"自动拾取开关",
nameof(Config.HotKeyConfig.AutoPickEnabledHotkey),
Config.HotKeyConfig.AutoPickEnabledHotkey,
hotKey =>
(_, _) =>
{
TaskContext.Instance().Config.AutoPickConfig.Enabled = !TaskContext.Instance().Config.AutoPickConfig.Enabled;
_logger.LogInformation("切换{Name}状态为[{Enabled}]", "自动拾取", ToChinese(TaskContext.Instance().Config.AutoPickConfig.Enabled));
Expand All @@ -76,7 +76,7 @@ private void BuildHotKeySettingModelList()
"自动剧情开关",
nameof(Config.HotKeyConfig.AutoSkipEnabledHotkey),
Config.HotKeyConfig.AutoSkipEnabledHotkey,
hotKey =>
(_, _) =>
{
TaskContext.Instance().Config.AutoSkipConfig.Enabled = !TaskContext.Instance().Config.AutoSkipConfig.Enabled;
_logger.LogInformation("切换{Name}状态为[{Enabled}]", "自动剧情", ToChinese(TaskContext.Instance().Config.AutoSkipConfig.Enabled));
Expand All @@ -88,7 +88,7 @@ private void BuildHotKeySettingModelList()
"自动钓鱼开关",
nameof(Config.HotKeyConfig.AutoFishingEnabledHotkey),
Config.HotKeyConfig.AutoFishingEnabledHotkey,
hotKey =>
(_, _) =>
{
TaskContext.Instance().Config.AutoFishingConfig.Enabled = !TaskContext.Instance().Config.AutoFishingConfig.Enabled;
_logger.LogInformation("切换{Name}状态为[{Enabled}]", "自动钓鱼", ToChinese(TaskContext.Instance().Config.AutoFishingConfig.Enabled));
Expand All @@ -100,23 +100,23 @@ private void BuildHotKeySettingModelList()
"长按旋转视角 - 那维莱特转圈",
nameof(Config.HotKeyConfig.TurnAroundHotkey),
Config.HotKeyConfig.TurnAroundHotkey,
hotKey => { TurnAroundMacro.Done(); }
(_, _) => { TurnAroundMacro.Done(); }
);
HotKeySettingModels.Add(turnAroundHotKeySettingModel);

var enhanceArtifactHotKeySettingModel = new HotKeySettingModel(
"按下快速强化圣遗物",
nameof(Config.HotKeyConfig.EnhanceArtifactHotkey),
Config.HotKeyConfig.EnhanceArtifactHotkey,
hotKey => { QuickEnhanceArtifactMacro.Done(); }
(_, _) => { QuickEnhanceArtifactMacro.Done(); }
);
HotKeySettingModels.Add(enhanceArtifactHotKeySettingModel);

HotKeySettingModels.Add(new HotKeySettingModel(
"启动/停止自动七圣召唤",
nameof(Config.HotKeyConfig.AutoGeniusInvokation),
Config.HotKeyConfig.AutoGeniusInvokation,
hotKey => { _taskSettingsPageViewModel.OnSwitchAutoGeniusInvokation(); }
(_, _) => { _taskSettingsPageViewModel.OnSwitchAutoGeniusInvokation(); }
));
}

Expand Down
17 changes: 17 additions & 0 deletions Fischless.HotkeyCapture/Fischless.HotkeyCapture.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net7.0-windows10.0.22621.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<Platforms>x64</Platforms>
<LangVersion>12.0</LangVersion>
<UseWindowsForms>True</UseWindowsForms>
<AllowUnsafeBlocks>True</AllowUnsafeBlocks>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Vanara.PInvoke.User32" Version="3.4.17" />
</ItemGroup>

</Project>
100 changes: 100 additions & 0 deletions Fischless.HotkeyCapture/Hotkey.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
using Vanara.PInvoke;

namespace Fischless.HotkeyCapture;

public sealed class Hotkey
{
public bool Alt { get; set; }
public bool Control { get; set; }
public bool Shift { get; set; }
public bool Windows { get; set; }

private Keys key;

public Keys Key
{
get => key;
set
{
if (value != Keys.ControlKey && value != Keys.Alt && value != Keys.Menu && value != Keys.ShiftKey)
{
key = value;
}
else
{
key = Keys.None;
}
}
}

public User32.HotKeyModifiers ModifierKey =>
(Windows ? User32.HotKeyModifiers.MOD_WIN : User32.HotKeyModifiers.MOD_NONE) |
(Control ? User32.HotKeyModifiers.MOD_CONTROL : User32.HotKeyModifiers.MOD_NONE) |
(Shift ? User32.HotKeyModifiers.MOD_SHIFT : User32.HotKeyModifiers.MOD_NONE) |
(Alt ? User32.HotKeyModifiers.MOD_ALT : User32.HotKeyModifiers.MOD_NONE);

public Hotkey()
{
Reset();
}

public Hotkey(string hotkeyStr)
{
try
{
string[] keyStrs = hotkeyStr.Replace(" ", string.Empty).Split('+');

foreach (string keyStr in keyStrs)
{
if (keyStr.Equals("Win", StringComparison.OrdinalIgnoreCase))
{
Windows = true;
}
else if (keyStr.Equals("Ctrl", StringComparison.OrdinalIgnoreCase))
{
Control = true;
}
else if (keyStr.Equals("Shift", StringComparison.OrdinalIgnoreCase))
{
Shift = true;
}
else if (keyStr.Equals("Alt", StringComparison.OrdinalIgnoreCase))
{
Alt = true;
}
else
{
Key = (Keys)Enum.Parse(typeof(Keys), keyStr);
}
}
}
catch
{
throw new ArgumentException("Invalid Hotkey");
}
}

public override string ToString()
{
string str = string.Empty;
if (Key != Keys.None)
{
str = string.Format("{0}{1}{2}{3}{4}",
Windows ? "Win + " : string.Empty,
Control ? "Ctrl + " : string.Empty,
Shift ? "Shift + " : string.Empty,
Alt ? "Alt + " : string.Empty,
Key);
}
return str;
}

public void Reset()
{
Alt = false;
Control = false;
Shift = false;
Windows = false;
Key = Keys.None;
}
}
41 changes: 41 additions & 0 deletions Fischless.HotkeyCapture/HotkeyHolder.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
namespace Fischless.HotkeyCapture;

public sealed class HotkeyHolder
{
private static Hotkey? hotkey;
private static HotkeyHook? hotkeyHook;
private static Action<object?, KeyPressedEventArgs>? keyPressed;

public static void RegisterHotKey(string hotkeyStr, Action<object?, KeyPressedEventArgs> keyPressed = null!)
{
if (string.IsNullOrEmpty(hotkeyStr))
{
UnregisterHotKey();
return;
}

hotkey = new Hotkey(hotkeyStr);

hotkeyHook?.Dispose();
hotkeyHook = new HotkeyHook();
hotkeyHook.KeyPressed -= OnKeyPressed;
hotkeyHook.KeyPressed += OnKeyPressed;
HotkeyHolder.keyPressed = keyPressed;
hotkeyHook.RegisterHotKey(hotkey.ModifierKey, hotkey.Key);
}

public static void UnregisterHotKey()
{
if (hotkeyHook != null)
{
hotkeyHook.KeyPressed -= OnKeyPressed;
hotkeyHook.UnregisterHotKey();
hotkeyHook.Dispose();
}
}

private static void OnKeyPressed(object? sender, KeyPressedEventArgs e)
{
keyPressed?.Invoke(sender, e);
}
}
Loading

0 comments on commit cef6f6a

Please sign in to comment.