This commit is contained in:
辉鸭蛋
2024-12-24 00:08:34 +08:00
parent 839343cebf
commit 54f7aab45c
5 changed files with 124 additions and 0 deletions

View File

@@ -0,0 +1,51 @@
using System;
using System.Runtime.InteropServices;
namespace BetterGenshinImpact.Helpers.Device;
public class MouseSettings
{
// Import the SystemParametersInfo function from the user32.dll
[DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)]
private static extern bool SystemParametersInfo(
uint uiAction, uint uiParam, ref uint pvParam, uint fWinIni);
// Constants for SystemParametersInfo function
private const uint SPI_GETMOUSESPEED = 0x0070;
private const uint SPI_SETMOUSESPEED = 0x0071;
private const uint SPI_GETWHEELSCROLLLINES = 0x0068;
private const uint SPI_SETWHEELSCROLLLINES = 0x0069;
// Get the current mouse speed
public static uint GetMouseSpeed()
{
uint mouseSpeed = 0;
SystemParametersInfo(SPI_GETMOUSESPEED, 0, ref mouseSpeed, 0);
return mouseSpeed;
}
// Set the mouse speed
public static void SetMouseSpeed(uint speed)
{
if (speed < 1 || speed > 20)
{
throw new ArgumentOutOfRangeException(nameof(speed), "Mouse speed must be between 1 and 20.");
}
SystemParametersInfo(SPI_SETMOUSESPEED, speed, ref speed, 0);
}
// Get the current mouse wheel scroll lines
public static uint GetMouseWheelScrollLines()
{
uint scrollLines = 0;
SystemParametersInfo(SPI_GETWHEELSCROLLLINES, 0, ref scrollLines, 0);
return scrollLines;
}
// Set the mouse wheel scroll lines
public static void SetMouseWheelScrollLines(uint lines)
{
SystemParametersInfo(SPI_SETWHEELSCROLLLINES, lines, ref lines, 0);
}
}

View File

@@ -0,0 +1,27 @@
using System;
using System.Runtime.InteropServices;
namespace BetterGenshinImpact.Helpers.Device;
public class MouseSpeedSettings
{
// Import the SystemParametersInfo function from the user32.dll
[DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)]
private static extern bool SystemParametersInfo(
uint uiAction, uint uiParam, uint pvParam, uint fWinIni);
// Constants for SystemParametersInfo function
private const uint SPI_GETMOUSESPEED = 0x0070;
private const uint SPI_SETMOUSESPEED = 0x0071;
public static void SetMouseSpeed(uint speed)
{
if (speed < 1 || speed > 20)
{
throw new ArgumentOutOfRangeException(nameof(speed), "Mouse speed must be between 1 and 20.");
}
SystemParametersInfo(SPI_SETMOUSESPEED, 0, speed, 0);
}
}

View File

@@ -0,0 +1,35 @@
using BetterGenshinImpact.GameTask.Common;
using Microsoft.Extensions.Logging;
namespace BetterGenshinImpact.Helpers.Device;
public class SystemSettingsManager
{
// 1. 在启动后强制设置成鼠标灵敏度设置10 滚轮一次滚动行数3结束后还原
private static uint _mouseSpeed = 10;
private static uint _wheelScrollLines = 3;
public static void GetSystemSettings()
{
_mouseSpeed = EnvironmentUtil.GetMouseSpeed(); // 获取鼠标灵敏度
_wheelScrollLines = EnvironmentUtil.GetWheelScrollLines(); // 获取滚轮滚动行数
TaskControl.Logger.LogDebug("当前鼠标灵敏度:{M1}, 滚轮滚动行数 {M2}", _mouseSpeed, _wheelScrollLines);
}
public static void SetSystemSettings()
{
MouseSpeedSettings.SetMouseSpeed(10); // 设置鼠标灵敏度
MouseSettings.SetMouseWheelScrollLines(3); // 设置滚轮滚动行数
TaskControl.Logger.LogDebug("强制设置后,当前鼠标灵敏度:{M1}, 滚轮滚动行数 {M2}", EnvironmentUtil.GetMouseSpeed(), EnvironmentUtil.GetWheelScrollLines());
}
public static void RestoreSystemSettings()
{
MouseSpeedSettings.SetMouseSpeed(_mouseSpeed); // 设置鼠标灵敏度
MouseSettings.SetMouseWheelScrollLines(_wheelScrollLines); // 设置滚轮滚动行数
TaskControl.Logger.LogDebug("还原设置后,当前鼠标灵敏度:{M1}, 滚轮滚动行数 {M2}", EnvironmentUtil.GetMouseSpeed(), EnvironmentUtil.GetWheelScrollLines());
}
}

View File

@@ -18,6 +18,7 @@ using System.Net.Http;
using System.Net.Http.Json;
using System.Threading.Tasks;
using System.Windows;
using BetterGenshinImpact.Helpers.Device;
using Wpf.Ui;
namespace BetterGenshinImpact.ViewModel;
@@ -119,7 +120,9 @@ public partial class MainWindowViewModel : ObservableObject, IViewModel
// 更新仓库
// ScriptRepoUpdater.Instance.AutoUpdate();
EnvironmentUtil.PrintMouseSettings();
}
private async Task GetNewestInfoAsync()

View File

@@ -19,6 +19,7 @@ using System.Threading.Tasks;
using System.Windows;
using BetterGenshinImpact.Core.Recorder.Model;
using BetterGenshinImpact.Helpers;
using BetterGenshinImpact.Helpers.Device;
using Wpf.Ui;
using Wpf.Ui.Controls;
using Wpf.Ui.Violeta.Controls;
@@ -122,10 +123,16 @@ public partial class KeyMouseRecordPageViewModel : ObservableObject, INavigation
return;
}
}
if (!IsRecording)
{
IsRecording = true;
SystemSettingsManager.GetSystemSettings();
SystemSettingsManager.SetSystemSettings();
fileName = $"{DateTime.Now:yyyy_MM_dd_HH_mm_ss}";
await GlobalKeyMouseRecord.Instance.StartRecord(fileName);
}
@@ -150,6 +157,7 @@ public partial class KeyMouseRecordPageViewModel : ObservableObject, INavigation
_logger.LogDebug(e, "停止录制时发生异常");
_logger.LogWarning(e.Message);
}
SystemSettingsManager.RestoreSystemSettings();
}
}