Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
62 changes: 47 additions & 15 deletions Ink Canvas/Helpers/GlobalHotkeyManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,14 @@ public class GlobalHotkeyManager : IDisposable
private readonly MainWindow _mainWindow;
private bool _isDisposed;
private bool _hotkeysShouldBeRegistered = true; // 启动时注册热键
private bool _isContextHotkeyDisabled;
private bool _allowContextExemptRegistration;

private static readonly HashSet<string> DrawingHotkeyNames = new HashSet<string>(StringComparer.Ordinal)
{
"DrawTool", "EraserTool", "BlackboardTool", "QuitDrawTool",
"Pen1", "Pen2", "Pen3", "Pen4", "Pen5", "DrawLine"
};

// 多屏幕支持相关字段
private Screen _currentScreen;
Expand Down Expand Up @@ -70,7 +78,7 @@ public bool RegisterHotkey(string hotkeyName, Key key, ModifierKeys modifiers, A
return false;

// 检查是否应该注册热键(基于屏幕和模式)
if (!ShouldRegisterHotkeys())
if (!ShouldRegisterHotkeys() && !_allowContextExemptRegistration)
{
return false;
}
Expand Down Expand Up @@ -471,6 +479,7 @@ public void EnableHotkeyRegistration()
{
try
{
_isContextHotkeyDisabled = false;
if (!_hotkeysShouldBeRegistered)
{
_hotkeysShouldBeRegistered = true;
Expand Down Expand Up @@ -512,22 +521,17 @@ public void DisableHotkeyRegistration()
{
try
{
if (_hotkeysShouldBeRegistered)
{
_hotkeysShouldBeRegistered = false;

// 停止鼠标位置监控定时器
if (_mousePositionTimer != null && _mousePositionTimer.IsEnabled)
{
_mousePositionTimer.Stop();
}
_isContextHotkeyDisabled = false;
_hotkeysShouldBeRegistered = false;

// 注销所有快捷键(含插件热键——这是用户/插件的显式停用)
UnregisterAllHotkeys(true);
}
else
// 停止鼠标位置监控定时器
if (_mousePositionTimer != null && _mousePositionTimer.IsEnabled)
{
_mousePositionTimer.Stop();
}

// 鼠标模式可能仍保留绘图热键,显式停用必须无条件注销全部热键。
UnregisterAllHotkeys(true);
}
catch (Exception ex)
{
Expand All @@ -545,11 +549,12 @@ private void DisableHotkeyRegistrationForContext()
{
try
{
if (!_hotkeysShouldBeRegistered)
if (!_hotkeysShouldBeRegistered && !_isContextHotkeyDisabled)
{
return;
}

_isContextHotkeyDisabled = true;
_hotkeysShouldBeRegistered = false;

// 停止鼠标位置监控定时器
Expand All @@ -560,13 +565,40 @@ private void DisableHotkeyRegistrationForContext()

// 仅注销内置快捷键,保留插件热键
UnregisterAllHotkeys(false);

if (MainWindow.Settings.Appearance.KeepDrawingHotkeysInMouseMode)
{
RegisterDrawingHotkeysForMouseMode();
}
}
catch (Exception ex)
{
LogHelper.WriteLogToFile($"上下文停用快捷键时出错: {ex.Message}", LogHelper.LogType.Error);
}
}

private void RegisterDrawingHotkeysForMouseMode()
{
try
{
var drawingHotkeys = GetHotkeysFromConfigFile();
_allowContextExemptRegistration = true;
foreach (var hotkey in drawingHotkeys)
{
if (!DrawingHotkeyNames.Contains(hotkey.Name))
continue;

var action = GetActionByName(hotkey.Name);
if (action != null)
RegisterHotkey(hotkey.Name, hotkey.Key, hotkey.Modifiers, action);
}
}
finally
{
_allowContextExemptRegistration = false;
}
}

/// <summary>
/// 根据当前工具模式更新快捷键状态
/// 在工具切换时调用此方法
Expand Down
4 changes: 4 additions & 0 deletions Ink Canvas/Properties/HotkeyStrings.Designer.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions Ink Canvas/Properties/HotkeyStrings.en-US.resx
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,12 @@
<data name="Hotkey_EnableHotkeysInMouseModeHint" xml:space="preserve">
<value>When on, hotkeys will work even in mouse mode</value>
</data>
<data name="Hotkey_KeepDrawingHotkeysInMouseMode" xml:space="preserve">
<value>Keep drawing hotkeys in mouse mode</value>
</data>
<data name="Hotkey_KeepDrawingHotkeysInMouseModeHint" xml:space="preserve">
<value>When the option above is off, keep drawing tool hotkeys to enter annotation mode; other hotkeys remain available to the foreground app</value>
</data>
<data name="Hotkey_PassThroughMouseWheelInDrawingMode" xml:space="preserve">
<value>Pass-through mouse wheel in drawing mode</value>
</data>
Expand Down
6 changes: 6 additions & 0 deletions Ink Canvas/Properties/HotkeyStrings.resx
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,12 @@
<data name="Hotkey_EnableHotkeysInMouseModeHint" xml:space="preserve">
<value>开启后,即使在鼠标模式下快捷键也会生效</value>
</data>
<data name="Hotkey_KeepDrawingHotkeysInMouseMode" xml:space="preserve">
<value>鼠标模式下保留绘图快捷键</value>
</data>
<data name="Hotkey_KeepDrawingHotkeysInMouseModeHint" xml:space="preserve">
<value>即使关闭上方选项时,仍启用绘图工具快捷键,使从鼠标模式进入批注;其他快捷键继续交给前台应用</value>
</data>
<data name="Hotkey_PassThroughMouseWheelInDrawingMode" xml:space="preserve">
<value>绘制模式下滚轮穿透</value>
</data>
Expand Down
6 changes: 6 additions & 0 deletions Ink Canvas/Properties/HotkeyStrings.zh-ME.resx
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,12 @@
<data name="Hotkey_EnableHotkeysInMouseModeHint" xml:space="preserve">
<value>开启后,鼠标形态下快捷键照样生效,不放过任何一个骚操作</value>
</data>
<data name="Hotkey_KeepDrawingHotkeysInMouseMode" xml:space="preserve">
<value>鼠标形态绘图骚操作照样生效</value>
</data>
<data name="Hotkey_KeepDrawingHotkeysInMouseModeHint" xml:space="preserve">
<value>关闭上方选项时,依然阔以用绘图骚操作进入批注招式,其他快捷键不受影响,中</value>
</data>
<data name="Hotkey_PassThroughMouseWheelInDrawingMode" xml:space="preserve">
<value>画画时让鼠标滚轮穿透下去</value>
</data>
Expand Down
2 changes: 2 additions & 0 deletions Ink Canvas/Properties/Strings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -573,6 +573,8 @@ public static class Strings
dict["Hotkey_MouseMode"] = ("HotkeyStrings", "Hotkey_MouseMode");
dict["Hotkey_EnableHotkeysInMouseMode"] = ("HotkeyStrings", "Hotkey_EnableHotkeysInMouseMode");
dict["Hotkey_EnableHotkeysInMouseModeHint"] = ("HotkeyStrings", "Hotkey_EnableHotkeysInMouseModeHint");
dict["Hotkey_KeepDrawingHotkeysInMouseMode"] = ("HotkeyStrings", "Hotkey_KeepDrawingHotkeysInMouseMode");
dict["Hotkey_KeepDrawingHotkeysInMouseModeHint"] = ("HotkeyStrings", "Hotkey_KeepDrawingHotkeysInMouseModeHint");
dict["Hotkey_PassThroughMouseWheelInDrawingMode"] = ("HotkeyStrings", "Hotkey_PassThroughMouseWheelInDrawingMode");
dict["Hotkey_PassThroughMouseWheelInDrawingModeHint"] = ("HotkeyStrings", "Hotkey_PassThroughMouseWheelInDrawingModeHint");
dict["Hotkey_EnablePPTPageKeyHook"] = ("HotkeyStrings", "Hotkey_EnablePPTPageKeyHook");
Expand Down
3 changes: 3 additions & 0 deletions Ink Canvas/Resources/Settings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -895,6 +895,9 @@ public class Appearance
[JsonProperty("enableHotkeysInMouseMode")]
public bool EnableHotkeysInMouseMode { get; set; } = false;

[JsonProperty("keepDrawingHotkeysInMouseMode")]
public bool KeepDrawingHotkeysInMouseMode { get; set; } = false;

[JsonProperty("passThroughMouseWheelInDrawingMode")]
public bool PassThroughMouseWheelInDrawingMode { get; set; } = false;

Expand Down
7 changes: 7 additions & 0 deletions Ink Canvas/Windows/SettingsViews/Pages/HotkeyPage.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,13 @@
SwitchName="ToggleSwitchEnableHotkeysInMouseMode"
Toggled="ToggleSwitchEnableHotkeysInMouseMode_Toggled" />

<controls:LabeledSettingsCard x:Name="CardKeepDrawingHotkeysInMouseMode"
Header="{x:Static props:HotkeyStrings.Hotkey_KeepDrawingHotkeysInMouseMode}"
Description="{x:Static props:HotkeyStrings.Hotkey_KeepDrawingHotkeysInMouseModeHint}"
Icon="{x:Static ui:SegoeFluentIcons.PenWorkspace}"
SwitchName="ToggleSwitchKeepDrawingHotkeysInMouseMode"
Toggled="ToggleSwitchKeepDrawingHotkeysInMouseMode_Toggled" />

<controls:LabeledSettingsCard x:Name="CardPassThroughMouseWheelInDrawingMode"
Header="{x:Static props:HotkeyStrings.Hotkey_PassThroughMouseWheelInDrawingMode}"
Description="{x:Static props:HotkeyStrings.Hotkey_PassThroughMouseWheelInDrawingModeHint}"
Expand Down
31 changes: 22 additions & 9 deletions Ink Canvas/Windows/SettingsViews/Pages/HotkeyPage.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -159,10 +159,30 @@ private void SetupEventHandlers()
private void LoadMouseModeSetting()
{
CardEnableHotkeysInMouseMode.IsOn = SettingsManager.Settings.Appearance.EnableHotkeysInMouseMode;
CardKeepDrawingHotkeysInMouseMode.IsOn = SettingsManager.Settings.Appearance.KeepDrawingHotkeysInMouseMode;
CardPassThroughMouseWheelInDrawingMode.IsOn = SettingsManager.Settings.Appearance.PassThroughMouseWheelInDrawingMode;
CardEnablePPTPageKeyHook.IsOn = SettingsManager.Settings.Appearance.EnablePPTPageKeyHook;
}

private void ToggleSwitchKeepDrawingHotkeysInMouseMode_Toggled(object sender, RoutedEventArgs e)
{
if (!_isLoaded) return;
try
{
SettingsManager.Settings.Appearance.KeepDrawingHotkeysInMouseMode = CardKeepDrawingHotkeysInMouseMode.IsOn;
SettingsManager.SaveSettingsToFile();
if (_hotkeyManager != null && _mainWindow != null)
{
bool isMouseMode = _mainWindow.inkCanvas.EditingMode == InkCanvasEditingMode.None;
_hotkeyManager.UpdateHotkeyStateForToolMode(isMouseMode);
}
}
catch (Exception ex)
{
LogHelper.WriteLogToFile($"更新鼠标模式绘图快捷键设置时出错: {ex.Message}", LogHelper.LogType.Error);
}
}

private void ToggleSwitchEnableHotkeysInMouseMode_Toggled(object sender, RoutedEventArgs e)
{
if (!_isLoaded) return;
Expand All @@ -175,14 +195,7 @@ private void ToggleSwitchEnableHotkeysInMouseMode_Toggled(object sender, RoutedE
if (_hotkeyManager != null && _mainWindow != null)
{
bool isCurrentlyMouseMode = _mainWindow.inkCanvas.EditingMode == InkCanvasEditingMode.None;
if (isCurrentlyMouseMode && !newState)
{
_hotkeyManager.DisableHotkeyRegistration();
}
else
{
_hotkeyManager.UpdateHotkeyStateForToolMode(isCurrentlyMouseMode);
}
_hotkeyManager.UpdateHotkeyStateForToolMode(isCurrentlyMouseMode);
}
}
catch (Exception ex)
Expand Down Expand Up @@ -398,4 +411,4 @@ private void BtnSave_Click(object sender, RoutedEventArgs e)
}
}
}
}
}
Loading