From d0793c546dfb47a3bbf15338f4706e1b39dbf3d6 Mon Sep 17 00:00:00 2001 From: CJKmkp <2564608840@qq.com> Date: Sat, 6 Sep 2025 14:36:07 +0800 Subject: [PATCH] =?UTF-8?q?improve:=E5=BF=AB=E6=8D=B7=E9=94=AE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Ink Canvas/Helpers/GlobalHotkeyManager.cs | 264 +++++++++++++++++- Ink Canvas/MainWindow_cs/MW_PPT.cs | 2 +- Ink Canvas/Resources/Settings.cs | 3 + Ink Canvas/Windows/HotkeySettingsWindow.xaml | 14 + .../Windows/HotkeySettingsWindow.xaml.cs | 44 +++ ...vasForClass.csproj.AssemblyReference.cache | Bin 23069 -> 35826 bytes 6 files changed, 323 insertions(+), 4 deletions(-) diff --git a/Ink Canvas/Helpers/GlobalHotkeyManager.cs b/Ink Canvas/Helpers/GlobalHotkeyManager.cs index b5ba3f40..046ac0e9 100644 --- a/Ink Canvas/Helpers/GlobalHotkeyManager.cs +++ b/Ink Canvas/Helpers/GlobalHotkeyManager.cs @@ -3,6 +3,7 @@ using NHotkey.Wpf; using System; using System.Collections.Generic; using System.IO; +using System.Linq; using System.Reflection; using System.Text; using System.Windows.Input; @@ -19,6 +20,7 @@ namespace Ink_Canvas.Helpers private readonly MainWindow _mainWindow; private bool _isDisposed; private bool _hotkeysShouldBeRegistered = true; // 启动时注册热键 + private bool _enableHotkeysInMouseMode = false; // 是否在鼠标模式下启用快捷键 // 配置文件路径 private static readonly string HotkeyConfigFile = Path.Combine(App.RootPath, "HotkeyConfig.json"); @@ -402,9 +404,19 @@ namespace Ink_Canvas.Helpers { if (isMouseMode) { - // 鼠标模式下禁用快捷键,让键盘操作放行 - DisableHotkeyRegistration(); - LogHelper.WriteLogToFile("切换到鼠标模式,禁用快捷键以放行键盘操作"); + // 鼠标模式下根据设置决定是否禁用快捷键 + if (_enableHotkeysInMouseMode) + { + // 如果启用了鼠标模式快捷键,则保持快捷键注册 + EnableHotkeyRegistration(); + LogHelper.WriteLogToFile("切换到鼠标模式,但保持快捷键启用(用户设置)"); + } + else + { + // 如果未启用鼠标模式快捷键,则禁用快捷键以放行键盘操作 + DisableHotkeyRegistration(); + LogHelper.WriteLogToFile("切换到鼠标模式,禁用快捷键以放行键盘操作"); + } } else { @@ -808,6 +820,251 @@ namespace Ink_Canvas.Helpers } } + /// + /// 设置是否在鼠标模式下启用快捷键 + /// + /// 是否启用 + public void SetEnableHotkeysInMouseMode(bool enable) + { + try + { + _enableHotkeysInMouseMode = enable; + + // 保存到主设置配置文件 + SaveMouseModeHotkeySettingToMainConfig(); + + LogHelper.WriteLogToFile($"设置鼠标模式快捷键开关: {enable}"); + + // 立即应用设置,如果当前是鼠标模式则重新更新状态 + var isCurrentlyMouseMode = IsInSelectMode(); + if (isCurrentlyMouseMode) + { + UpdateHotkeyStateForToolMode(true); + } + } + catch (Exception ex) + { + LogHelper.WriteLogToFile($"设置鼠标模式快捷键开关时出错: {ex.Message}", LogHelper.LogType.Error); + } + } + + /// + /// 获取是否在鼠标模式下启用快捷键 + /// + /// 是否启用 + public bool GetEnableHotkeysInMouseMode() + { + try + { + // 从主设置配置文件加载设置 + LoadMouseModeHotkeySettingFromMainConfig(); + return _enableHotkeysInMouseMode; + } + catch (Exception ex) + { + LogHelper.WriteLogToFile($"获取鼠标模式快捷键开关时出错: {ex.Message}", LogHelper.LogType.Error); + return false; // 默认关闭 + } + } + + /// + /// 从主设置配置文件加载鼠标模式快捷键设置 + /// + private void LoadMouseModeHotkeySettingFromMainConfig() + { + try + { + // 通过反射访问主窗口的Settings属性 + var settingsProperty = _mainWindow.GetType().GetProperty("Settings", BindingFlags.Public | BindingFlags.Instance); + if (settingsProperty != null) + { + var settings = settingsProperty.GetValue(_mainWindow); + if (settings != null) + { + // 获取Advanced属性 + var advancedProperty = settings.GetType().GetProperty("Advanced"); + if (advancedProperty != null) + { + var advanced = advancedProperty.GetValue(settings); + if (advanced != null) + { + // 获取EnableHotkeysInMouseMode属性 + var enableHotkeysProperty = advanced.GetType().GetProperty("EnableHotkeysInMouseMode"); + if (enableHotkeysProperty != null) + { + var value = enableHotkeysProperty.GetValue(advanced); + if (value is bool boolValue) + { + _enableHotkeysInMouseMode = boolValue; + LogHelper.WriteLogToFile($"从主设置配置文件加载鼠标模式快捷键设置: {_enableHotkeysInMouseMode}"); + return; + } + } + } + } + } + } + + // 如果无法从主设置加载,使用默认值 + _enableHotkeysInMouseMode = false; + LogHelper.WriteLogToFile("无法从主设置配置文件加载鼠标模式快捷键设置,使用默认值: false"); + } + catch (Exception ex) + { + LogHelper.WriteLogToFile($"从主设置配置文件加载鼠标模式快捷键设置时出错: {ex.Message}", LogHelper.LogType.Error); + _enableHotkeysInMouseMode = false; // 默认关闭 + } + } + + /// + /// 从配置文件加载鼠标模式快捷键设置(旧方法,保留兼容性) + /// + private void LoadMouseModeHotkeySetting() + { + try + { + if (!File.Exists(HotkeyConfigFile)) + { + _enableHotkeysInMouseMode = false; // 默认关闭 + return; + } + + var json = File.ReadAllText(HotkeyConfigFile, Encoding.UTF8); + var config = JsonConvert.DeserializeObject(json); + + if (config != null) + { + // 查找鼠标模式快捷键设置 + var mouseModeSetting = config.Hotkeys?.FirstOrDefault(h => h.Name == "EnableHotkeysInMouseMode"); + if (mouseModeSetting != null) + { + _enableHotkeysInMouseMode = mouseModeSetting.EnableHotkeysInMouseMode; + LogHelper.WriteLogToFile($"从配置文件加载鼠标模式快捷键设置: {_enableHotkeysInMouseMode}"); + } + else + { + _enableHotkeysInMouseMode = false; // 默认关闭 + } + } + else + { + _enableHotkeysInMouseMode = false; // 默认关闭 + } + } + catch (Exception ex) + { + LogHelper.WriteLogToFile($"从配置文件加载鼠标模式快捷键设置时出错: {ex.Message}", LogHelper.LogType.Error); + _enableHotkeysInMouseMode = false; // 默认关闭 + } + } + + /// + /// 保存鼠标模式快捷键设置到主设置配置文件 + /// + private void SaveMouseModeHotkeySettingToMainConfig() + { + try + { + // 通过反射访问主窗口的Settings属性 + var settingsProperty = _mainWindow.GetType().GetProperty("Settings", BindingFlags.Public | BindingFlags.Instance); + if (settingsProperty != null) + { + var settings = settingsProperty.GetValue(_mainWindow); + if (settings != null) + { + // 获取Advanced属性 + var advancedProperty = settings.GetType().GetProperty("Advanced"); + if (advancedProperty != null) + { + var advanced = advancedProperty.GetValue(settings); + if (advanced != null) + { + // 设置EnableHotkeysInMouseMode属性 + var enableHotkeysProperty = advanced.GetType().GetProperty("EnableHotkeysInMouseMode"); + if (enableHotkeysProperty != null) + { + enableHotkeysProperty.SetValue(advanced, _enableHotkeysInMouseMode); + + // 触发主窗口的保存设置方法 + var saveSettingsMethod = _mainWindow.GetType().GetMethod("SaveSettings", BindingFlags.NonPublic | BindingFlags.Instance); + if (saveSettingsMethod != null) + { + saveSettingsMethod.Invoke(_mainWindow, null); + LogHelper.WriteLogToFile($"保存鼠标模式快捷键设置到主设置配置文件: {_enableHotkeysInMouseMode}"); + } + else + { + LogHelper.WriteLogToFile("无法找到SaveSettings方法", LogHelper.LogType.Warning); + } + } + } + } + } + } + } + catch (Exception ex) + { + LogHelper.WriteLogToFile($"保存鼠标模式快捷键设置到主设置配置文件时出错: {ex.Message}", LogHelper.LogType.Error); + } + } + + /// + /// 保存鼠标模式快捷键设置到配置文件(旧方法,保留兼容性) + /// + private void SaveMouseModeHotkeySetting() + { + try + { + HotkeyConfig config; + + if (File.Exists(HotkeyConfigFile)) + { + var json = File.ReadAllText(HotkeyConfigFile, Encoding.UTF8); + config = JsonConvert.DeserializeObject(json) ?? new HotkeyConfig(); + } + else + { + config = new HotkeyConfig(); + } + + // 确保Hotkeys列表存在 + if (config.Hotkeys == null) + { + config.Hotkeys = new List(); + } + + // 查找或创建鼠标模式快捷键设置项 + var mouseModeSetting = config.Hotkeys.FirstOrDefault(h => h.Name == "EnableHotkeysInMouseMode"); + if (mouseModeSetting == null) + { + mouseModeSetting = new HotkeyConfigItem + { + Name = "EnableHotkeysInMouseMode", + EnableHotkeysInMouseMode = _enableHotkeysInMouseMode + }; + config.Hotkeys.Add(mouseModeSetting); + } + else + { + mouseModeSetting.EnableHotkeysInMouseMode = _enableHotkeysInMouseMode; + } + + // 更新配置信息 + config.Version = "1.0"; + config.LastModified = DateTime.Now; + + // 保存到文件 + var jsonString = JsonConvert.SerializeObject(config, Formatting.Indented); + File.WriteAllText(HotkeyConfigFile, jsonString, Encoding.UTF8); + + LogHelper.WriteLogToFile($"保存鼠标模式快捷键设置到配置文件: {_enableHotkeysInMouseMode}"); + } + catch (Exception ex) + { + LogHelper.WriteLogToFile($"保存鼠标模式快捷键设置到配置文件时出错: {ex.Message}", LogHelper.LogType.Error); + } + } + #endregion @@ -858,6 +1115,7 @@ namespace Ink_Canvas.Helpers public string Name { get; set; } public Key Key { get; set; } public ModifierKeys Modifiers { get; set; } + public bool EnableHotkeysInMouseMode { get; set; } } #endregion } diff --git a/Ink Canvas/MainWindow_cs/MW_PPT.cs b/Ink Canvas/MainWindow_cs/MW_PPT.cs index 642d4868..2f887d58 100644 --- a/Ink Canvas/MainWindow_cs/MW_PPT.cs +++ b/Ink Canvas/MainWindow_cs/MW_PPT.cs @@ -84,7 +84,7 @@ namespace Ink_Canvas private DispatcherTimer _longPressTimer; private bool _isLongPressNext = true; // true为下一页,false为上一页 private const int LongPressDelay = 500; // 长按延迟时间(毫秒) - private const int LongPressInterval = 15; // 长按翻页间隔(毫秒) + private const int LongPressInterval = 50; // 长按翻页间隔(毫秒) // PowerPoint应用程序守护相关字段 private DispatcherTimer _powerPointProcessMonitorTimer; diff --git a/Ink Canvas/Resources/Settings.cs b/Ink Canvas/Resources/Settings.cs index 024b7788..d8c6ac3a 100644 --- a/Ink Canvas/Resources/Settings.cs +++ b/Ink Canvas/Resources/Settings.cs @@ -498,6 +498,9 @@ namespace Ink_Canvas [JsonProperty("isEnableAvoidFullScreenHelper")] public bool IsEnableAvoidFullScreenHelper { get; set; } + [JsonProperty("enableHotkeysInMouseMode")] + public bool EnableHotkeysInMouseMode { get; set; } = false; + [JsonProperty("isAutoBackupBeforeUpdate")] public bool IsAutoBackupBeforeUpdate { get; set; } = true; diff --git a/Ink Canvas/Windows/HotkeySettingsWindow.xaml b/Ink Canvas/Windows/HotkeySettingsWindow.xaml index 8b008b07..e8c41995 100644 --- a/Ink Canvas/Windows/HotkeySettingsWindow.xaml +++ b/Ink Canvas/Windows/HotkeySettingsWindow.xaml @@ -49,6 +49,20 @@ Margin="0,0,0,20" Foreground="#666666"/> + + + + + + + + diff --git a/Ink Canvas/Windows/HotkeySettingsWindow.xaml.cs b/Ink Canvas/Windows/HotkeySettingsWindow.xaml.cs index f4d1895e..f92949f9 100644 --- a/Ink Canvas/Windows/HotkeySettingsWindow.xaml.cs +++ b/Ink Canvas/Windows/HotkeySettingsWindow.xaml.cs @@ -153,6 +153,9 @@ namespace Ink_Canvas.Windows LogHelper.WriteLogToFile($"设置默认显示值: {hotkeyItem.HotkeyName}"); } } + + // 加载鼠标模式快捷键设置 + LoadMouseModeHotkeySetting(); } catch (Exception ex) { @@ -239,6 +242,9 @@ namespace Ink_Canvas.Windows { hotkeyItem.HotkeyChanged += OnHotkeyChanged; } + + // 为鼠标模式快捷键开关设置事件处理器 + EnableHotkeysInMouseModeToggle.Toggled += EnableHotkeysInMouseModeToggle_Toggled; } private void OnHotkeyChanged(object sender, HotkeyChangedEventArgs e) @@ -555,6 +561,44 @@ namespace Ink_Canvas.Windows MessageBox.Show($"保存快捷键设置时出错: {ex.Message}", "错误", MessageBoxButton.OK, MessageBoxImage.Error); } } + + /// + /// 鼠标模式快捷键开关切换事件 + /// + private void EnableHotkeysInMouseModeToggle_Toggled(object sender, RoutedEventArgs e) + { + try + { + var isEnabled = EnableHotkeysInMouseModeToggle.IsOn; + LogHelper.WriteLogToFile($"鼠标模式快捷键开关状态变更: {isEnabled}"); + + // 立即更新快捷键管理器的设置 + _hotkeyManager.SetEnableHotkeysInMouseMode(isEnabled); + } + catch (Exception ex) + { + LogHelper.WriteLogToFile($"处理鼠标模式快捷键开关切换时出错: {ex.Message}", LogHelper.LogType.Error); + } + } + + /// + /// 加载鼠标模式快捷键设置 + /// + private void LoadMouseModeHotkeySetting() + { + try + { + var isEnabled = _hotkeyManager.GetEnableHotkeysInMouseMode(); + EnableHotkeysInMouseModeToggle.IsOn = isEnabled; + LogHelper.WriteLogToFile($"从主设置配置文件加载鼠标模式快捷键设置: {isEnabled}"); + } + catch (Exception ex) + { + LogHelper.WriteLogToFile($"加载鼠标模式快捷键设置时出错: {ex.Message}", LogHelper.LogType.Error); + // 默认关闭 + EnableHotkeysInMouseModeToggle.IsOn = false; + } + } #endregion } diff --git a/Ink Canvas/obj/Debug/net472/InkCanvasForClass.csproj.AssemblyReference.cache b/Ink Canvas/obj/Debug/net472/InkCanvasForClass.csproj.AssemblyReference.cache index 771b061a32c98a1ec2de289e4866236dcb85d74a..44b9fc7383ebb573862944afd3220347eedadfaa 100644 GIT binary patch delta 2354 zcma)8&u<%55Y8?{4X%I0KjWArHKjiez$!&Z#7a@+#I3qfHjQkeL|oiuy$MfUzg>3M z1Q!Gi2U-vkP3FLf1AhTR&9$64_R#jswd#eoCr;tb!0_P z=Rc`;w{>Mi{3)a2U!1#so3*&9amPD}4}8<=``p6Mbck=>pg{CMk>9#EVyf~Q>{hSB znF}?zQmz+96h%olxFs*)ZVwndM1D znvh8K4i8z|*6McK4dGH{79PA6hlR7};qFRGabPyFc5mtABSlrli4Ec)qp1~r3UNvi zBW7-KHwc*(Xdn8fC7>>1u@WB2%R~A#ZP9Iez8wV2<$`);6vJisJ6neBnaVkQdP5$@ zlP(u68g9#WDA5gAC=1AaF?Dq>nzritP_FfBY6V1*IDy% zuaY1OXEV^uZ@~593HTv#N>yUir=Xt0%#_S**jC49VK>s7@=*vO2nm|&(dL_MtK*xJ zDNQGXP>HH#Vdoz+rw{1J;9j2V1RXSPWiI7FFfd-aZM^Sp-f>;98fPZDt)wttM|rDPq_(bR0gSK2ClSg@goUM4I}) z_BEXcq3OW0Intcwls1^~2-+A|a0&b?ds`5%dP5Szv(1gq5h5itZ z52>*6FX6}P64d5&cya0)?9bGZ>HxuI2OCh%$6-A^(bvhKEG$5Z7B=8vdRkS+h~}+n zl4ejQswU_TT&LkHbXYunGg?sGJ>-N0MB~E&&f}O)%Q3Ap-g37CF(9uKYQis=*Kuf_ z=+cmkg7c8&Bu=+8U#m*0CkAptZo6|U!#U9{LVa*-qKg@HN&cr3b^t3DGph|c9r#|O;ec)gGj?B!C~mg6Yo=c&cSi@ir0+pgc#6t4-xMMqBiqiMb`+winjV`{!YXVc_BXxAuk*z-ZnWRLfI*J66KgP z#?l9cPE>+3rMWi42m}?4_xNLCOw}8wy@C8n6Tp`miNuX z=aD#q;qc<5#aJ=yvkzHh6kT!XywmOjRog88~WAa6#{>h?d(#1;fOh3h=JJvP((Rked}{0giegEH