add:快捷键面板

This commit is contained in:
2026-05-01 17:36:53 +08:00
parent 6980abe331
commit 6c7c76958f
10 changed files with 612 additions and 1130 deletions
-23
View File
@@ -2575,29 +2575,6 @@ namespace Ink_Canvas
}
}
/// <summary>
/// 打开快捷键设置窗口
/// </summary>
private void OpenHotkeySettingsWindow()
{
try
{
if (_globalHotkeyManager == null)
{
MessageBox.Show("快捷键管理器尚未初始化,请稍后重试。", "错误", MessageBoxButton.OK, MessageBoxImage.Error);
return;
}
var hotkeySettingsWindow = new HotkeySettingsWindow(this, _globalHotkeyManager);
hotkeySettingsWindow.Owner = this;
hotkeySettingsWindow.ShowDialog();
}
catch (Exception ex)
{
LogHelper.WriteLogToFile($"打开快捷键设置窗口时出错: {ex.Message}", LogHelper.LogType.Error);
MessageBox.Show($"打开快捷键设置窗口时出错: {ex.Message}", "错误", MessageBoxButton.OK, MessageBoxImage.Error);
}
}
#endregion
#region /
+16 -47
View File
@@ -3,68 +3,37 @@
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:ui="http://schemas.inkore.net/lib/ui/wpf/modern"
xmlns:ikw="http://schemas.inkore.net/lib/ui/wpf"
mc:Ignorable="d"
d:DesignHeight="60"
d:DesignHeight="68"
d:DesignWidth="600">
<Border Background="#2d2d30"
BorderBrush="#3f3f46"
BorderThickness="1"
CornerRadius="6"
Margin="0,2"
SnapsToDevicePixels="True">
<Grid Margin="14,10">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*" />
<ColumnDefinition Width="Auto" />
<ColumnDefinition Width="Auto" />
</Grid.ColumnDefinitions>
<ikw:SimpleStackPanel Grid.Column="0" VerticalAlignment="Center">
<TextBlock x:Name="TitleTextBlock"
Text="快捷键标题"
FontSize="14"
FontWeight="SemiBold"
Foreground="#fafafa" />
<TextBlock x:Name="DescriptionTextBlock"
Text="快捷键描述"
FontSize="12"
Foreground="#a1a1aa"
Margin="0,2,0,0"
TextWrapping="Wrap" />
</ikw:SimpleStackPanel>
<Border Grid.Column="1"
Background="#3f3f46"
BorderBrush="#71717a"
<ui:SettingsCard x:Name="RootCard"
Header="{Binding Title, RelativeSource={RelativeSource AncestorType=UserControl}}"
Description="{Binding Description, RelativeSource={RelativeSource AncestorType=UserControl}}">
<ui:SettingsCard.HeaderIcon>
<ui:FontIcon x:Name="HeaderFontIcon" Icon="{x:Static ui:SegoeFluentIcons.KeyboardStandard}" />
</ui:SettingsCard.HeaderIcon>
<ikw:SimpleStackPanel Orientation="Horizontal" Spacing="8" VerticalAlignment="Center">
<Border Background="{DynamicResource SubtleFillColorSecondaryBrush}"
BorderBrush="{DynamicResource ControlStrokeColorDefaultBrush}"
BorderThickness="1"
CornerRadius="4"
Margin="10,0,0,0"
Padding="10,6"
Padding="10,4"
VerticalAlignment="Center">
<TextBlock x:Name="CurrentHotkeyTextBlock"
Text="未设置"
FontSize="12"
FontFamily="Consolas"
FontWeight="SemiBold"
Foreground="#fafafa"
MinWidth="88"
TextAlignment="Center"
VerticalAlignment="Center" />
</Border>
<Button x:Name="BtnSetHotkey"
Grid.Column="2"
Content="设置"
Width="72"
Height="30"
FontSize="12"
Margin="10,0,0,0"
VerticalAlignment="Center"
Style="{DynamicResource PrimaryButtonStyle}"
Padding="12,4"
MinWidth="64"
Click="BtnSetHotkey_Click" />
</Grid>
</Border>
</UserControl>
</ikw:SimpleStackPanel>
</ui:SettingsCard>
</UserControl>
+26 -57
View File
@@ -2,7 +2,6 @@ using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using System.Windows.Media;
namespace Ink_Canvas.Windows
{
@@ -11,34 +10,24 @@ namespace Ink_Canvas.Windows
/// </summary>
public partial class HotkeyItem : UserControl
{
private static readonly SolidColorBrush HotkeyValueForeground = CreateFrozenBrush(0xFA, 0xFA, 0xFA);
private static readonly SolidColorBrush HotkeyPlaceholderForeground = CreateFrozenBrush(0xA1, 0xA1, 0xAA);
public static readonly DependencyProperty TitleProperty =
DependencyProperty.Register(nameof(Title), typeof(string), typeof(HotkeyItem),
new PropertyMetadata(string.Empty));
private static SolidColorBrush CreateFrozenBrush(byte r, byte g, byte b)
{
var brush = new SolidColorBrush(Color.FromRgb(r, g, b));
brush.Freeze();
return brush;
}
public static readonly DependencyProperty DescriptionProperty =
DependencyProperty.Register(nameof(Description), typeof(string), typeof(HotkeyItem),
new PropertyMetadata(string.Empty));
#region Events
/// <summary>
/// 快捷键变更事件
/// </summary>
public event EventHandler<HotkeyChangedEventArgs> HotkeyChanged;
#endregion
#region Properties
public string Title
{
get => TitleTextBlock.Text;
set => TitleTextBlock.Text = value;
get => (string)GetValue(TitleProperty);
set => SetValue(TitleProperty, value);
}
public string Description
{
get => DescriptionTextBlock.Text;
set => DescriptionTextBlock.Text = value;
get => (string)GetValue(DescriptionProperty);
set => SetValue(DescriptionProperty, value);
}
public string DefaultKey { get; set; }
@@ -49,24 +38,20 @@ namespace Ink_Canvas.Windows
/// </summary>
public string HotkeyName { get; set; }
/// <summary>
/// 快捷键变更事件
/// </summary>
public event EventHandler<HotkeyChangedEventArgs> HotkeyChanged;
private Key _currentKey = Key.None;
private ModifierKeys _currentModifiers = ModifierKeys.None;
#endregion
#region Constructor
public HotkeyItem()
{
InitializeComponent();
UpdateHotkeyDisplay();
}
#endregion
#region Public Methods
/// <summary>
/// 设置当前快捷键
/// </summary>
/// <param name="key">按键</param>
/// <param name="modifiers">修饰键</param>
public void SetCurrentHotkey(Key key, ModifierKeys modifiers)
{
_currentKey = key;
@@ -74,41 +59,28 @@ namespace Ink_Canvas.Windows
UpdateHotkeyDisplay();
}
/// <summary>
/// 获取当前快捷键
/// </summary>
/// <returns>快捷键信息</returns>
public (Key key, ModifierKeys modifiers) GetCurrentHotkey()
{
return (_currentKey, _currentModifiers);
}
#endregion
#region Private Methods
private void UpdateHotkeyDisplay()
{
if (_currentKey == Key.None)
{
CurrentHotkeyTextBlock.Text = "未设置";
CurrentHotkeyTextBlock.Foreground = HotkeyPlaceholderForeground;
}
else
{
var modifiersText = _currentModifiers == ModifierKeys.None ? "" : $"{_currentModifiers}+";
CurrentHotkeyTextBlock.Text = $"{modifiersText}{_currentKey}";
CurrentHotkeyTextBlock.Foreground = HotkeyValueForeground;
}
}
private void StartHotkeyCapture()
{
BtnSetHotkey.Content = "请按键...";
BtnSetHotkey.Background = Brushes.Orange;
// 设置焦点以捕获键盘事件
Focus();
// 添加键盘事件处理器
KeyDown += HotkeyItem_KeyDown;
KeyUp += HotkeyItem_KeyUp;
}
@@ -116,9 +88,6 @@ namespace Ink_Canvas.Windows
private void StopHotkeyCapture()
{
BtnSetHotkey.Content = "设置";
BtnSetHotkey.ClearValue(Button.BackgroundProperty);
// 移除键盘事件处理器
KeyDown -= HotkeyItem_KeyDown;
KeyUp -= HotkeyItem_KeyUp;
}
@@ -127,7 +96,6 @@ namespace Ink_Canvas.Windows
{
e.Handled = true;
// 忽略某些特殊键
if (e.Key == Key.LeftShift || e.Key == Key.RightShift ||
e.Key == Key.LeftCtrl || e.Key == Key.RightCtrl ||
e.Key == Key.LeftAlt || e.Key == Key.RightAlt ||
@@ -136,7 +104,6 @@ namespace Ink_Canvas.Windows
return;
}
// 获取修饰键
var modifiers = ModifierKeys.None;
if (Keyboard.IsKeyDown(Key.LeftCtrl) || Keyboard.IsKeyDown(Key.RightCtrl))
modifiers |= ModifierKeys.Control;
@@ -147,20 +114,15 @@ namespace Ink_Canvas.Windows
if (Keyboard.IsKeyDown(Key.LWin) || Keyboard.IsKeyDown(Key.RWin))
modifiers |= ModifierKeys.Windows;
// 设置新的快捷键
var oldKey = _currentKey;
var oldModifiers = _currentModifiers;
_currentKey = e.Key;
_currentModifiers = modifiers;
UpdateHotkeyDisplay();
StopHotkeyCapture();
// 触发快捷键变更事件
HotkeyChanged?.Invoke(this, new HotkeyChangedEventArgs
{
HotkeyName = HotkeyName ?? Title, // 优先使用HotkeyName,如果没有则使用Title
HotkeyName = HotkeyName ?? Title,
Key = _currentKey,
Modifiers = _currentModifiers
});
@@ -170,13 +132,20 @@ namespace Ink_Canvas.Windows
{
e.Handled = true;
}
#endregion
#region Event Handlers
private void BtnSetHotkey_Click(object sender, RoutedEventArgs e)
{
StartHotkeyCapture();
}
#endregion
}
/// <summary>
/// 快捷键变更事件参数
/// </summary>
public class HotkeyChangedEventArgs : EventArgs
{
public string HotkeyName { get; set; }
public Key Key { get; set; }
public ModifierKeys Modifiers { get; set; }
}
}
@@ -1,375 +0,0 @@
<Window x:Class="Ink_Canvas.Windows.HotkeySettingsWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:Ink_Canvas.Windows"
xmlns:ui="http://schemas.inkore.net/lib/ui/wpf/modern"
xmlns:ikw="http://schemas.inkore.net/lib/ui/wpf"
mc:Ignorable="d"
Title="快捷键设置"
Height="600"
Width="800"
WindowStartupLocation="CenterScreen"
ResizeMode="CanResize"
FontFamily="Microsoft YaHei UI"
ui:ThemeManager.IsThemeAware="True"
ui:TitleBar.ExtendViewIntoTitleBar="True"
ui:WindowHelper.SystemBackdropType="Mica"
ui:WindowHelper.UseModernWindowStyle="True"
ui:TitleBar.Height="48">
<Window.Resources>
<ResourceDictionary>
<SolidColorBrush x:Key="WindowBackground" Color="#1e1e1e" />
<SolidColorBrush x:Key="BorderBrush" Color="#3f3f46" />
<SolidColorBrush x:Key="TextForeground" Color="#fafafa" />
<SolidColorBrush x:Key="TextSecondary" Color="#a1a1aa" />
<SolidColorBrush x:Key="AccentColor" Color="#3b82f6" />
<SolidColorBrush x:Key="TitleForeground" Color="#fafafa" />
<Style x:Key="PrimaryButtonStyle" TargetType="Button">
<Setter Property="Background" Value="{StaticResource AccentColor}" />
<Setter Property="Foreground" Value="White" />
<Setter Property="BorderThickness" Value="0" />
<Setter Property="FontWeight" Value="SemiBold" />
<Setter Property="Padding" Value="16,8" />
<Setter Property="Cursor" Value="Hand" />
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="Button">
<Border Background="{TemplateBinding Background}"
CornerRadius="4"
Padding="{TemplateBinding Padding}">
<ContentPresenter HorizontalAlignment="Center"
VerticalAlignment="Center" />
</Border>
<ControlTemplate.Triggers>
<Trigger Property="IsMouseOver" Value="True">
<Setter Property="Background" Value="#2563eb" />
</Trigger>
<Trigger Property="IsPressed" Value="True">
<Setter Property="Background" Value="#1d4ed8" />
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
<Style x:Key="SecondaryButtonStyle" TargetType="Button">
<Setter Property="Background" Value="Transparent" />
<Setter Property="Foreground" Value="{StaticResource TextForeground}" />
<Setter Property="BorderBrush" Value="{StaticResource BorderBrush}" />
<Setter Property="BorderThickness" Value="1" />
<Setter Property="FontWeight" Value="SemiBold" />
<Setter Property="Padding" Value="16,8" />
<Setter Property="Cursor" Value="Hand" />
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="Button">
<Border Background="{TemplateBinding Background}"
BorderBrush="{TemplateBinding BorderBrush}"
BorderThickness="{TemplateBinding BorderThickness}"
CornerRadius="4"
Padding="{TemplateBinding Padding}">
<ContentPresenter HorizontalAlignment="Center"
VerticalAlignment="Center" />
</Border>
<ControlTemplate.Triggers>
<Trigger Property="IsMouseOver" Value="True">
<Setter Property="Background" Value="#27272a" />
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</ResourceDictionary>
</Window.Resources>
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="Auto" />
<RowDefinition Height="*" />
<RowDefinition Height="Auto" />
</Grid.RowDefinitions>
<!-- 标题栏(与云存储管理等新设置窗口一致) -->
<Border x:Name="Border_TitleBarRoot"
Grid.Row="0"
Height="{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=Window}, Path=(ui:TitleBar.Height)}"
MouseLeftButtonDown="TitleBar_MouseLeftButtonDown">
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition />
<ColumnDefinition Width="Auto" />
<ColumnDefinition Width="Auto" />
</Grid.ColumnDefinitions>
<TextBlock Grid.Column="0"
Text="{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=Window}, Path=Title}"
VerticalAlignment="Center"
Margin="12,0,0,0"
FontSize="12"
FontWeight="SemiBold"
Foreground="{StaticResource TitleForeground}" />
<ikw:SimpleStackPanel x:Name="StackPanel_RightButtons"
Grid.Column="1"
Orientation="Horizontal"
Spacing="5"
VerticalAlignment="Center" />
<Rectangle Grid.Column="2"
Width="{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=Window}, Path=(ui:TitleBar.SystemOverlayRightInset)}" />
</Grid>
</Border>
<!-- 主内容 -->
<Border Grid.Row="1"
Background="{StaticResource WindowBackground}"
Padding="20,10,20,16">
<ui:ScrollViewerEx VerticalScrollBarVisibility="Auto"
HorizontalScrollBarVisibility="Disabled"
PanningMode="VerticalOnly"
ui:ThemeManager.RequestedTheme="Dark">
<ikw:SimpleStackPanel Spacing="14">
<TextBlock Text="在这里可以自定义全局快捷键设置。全局快捷键在任何情况下都能生效,即使应用程序不在焦点状态。"
TextWrapping="Wrap"
Foreground="{StaticResource TextSecondary}"
FontSize="14"
LineHeight="20" />
<Border BorderThickness="1"
BorderBrush="{StaticResource BorderBrush}"
CornerRadius="8"
Padding="18,16"
Background="#27272a">
<ikw:SimpleStackPanel Spacing="10">
<TextBlock Text="鼠标模式"
FontSize="18"
FontWeight="SemiBold"
Foreground="{StaticResource TextForeground}" />
<Border Height="1"
Background="{StaticResource BorderBrush}"
HorizontalAlignment="Stretch" />
<ikw:SimpleStackPanel Orientation="Horizontal" VerticalAlignment="Center">
<ui:ToggleSwitch x:Name="ToggleSwitchEnableHotkeysInMouseMode"
Header="在鼠标模式下启用快捷键"
OnContent=""
OffContent=""
FontWeight="Bold"
Margin="0,0,10,0"
Width="220" />
</ikw:SimpleStackPanel>
<TextBlock Text="开启后,即使在鼠标模式下快捷键也会生效"
Foreground="{StaticResource TextSecondary}"
TextWrapping="Wrap"
FontSize="13" />
</ikw:SimpleStackPanel>
</Border>
<ikw:SimpleStackPanel x:Name="HotkeyList" Spacing="12">
<!-- 基本操作 -->
<Border BorderThickness="1"
BorderBrush="{StaticResource BorderBrush}"
CornerRadius="8"
Padding="18,16"
Background="#27272a">
<ikw:SimpleStackPanel Spacing="10">
<TextBlock Text="基本操作"
FontSize="18"
FontWeight="SemiBold"
Foreground="{StaticResource TextForeground}" />
<Border Height="1"
Background="{StaticResource BorderBrush}"
HorizontalAlignment="Stretch" />
<ikw:SimpleStackPanel Spacing="4">
<local:HotkeyItem x:Name="UndoHotkey"
Title="撤销"
Description="撤销上一步操作"
DefaultKey="Z"
DefaultModifiers="Control" />
<local:HotkeyItem x:Name="RedoHotkey"
Title="重做"
Description="重做上一步操作"
DefaultKey="Y"
DefaultModifiers="Control" />
<local:HotkeyItem x:Name="ClearHotkey"
Title="清空"
Description="清空当前画板内容"
DefaultKey="E"
DefaultModifiers="Control" />
<local:HotkeyItem x:Name="PasteHotkey"
Title="粘贴"
Description="粘贴剪贴板内容"
DefaultKey="V"
DefaultModifiers="Control" />
</ikw:SimpleStackPanel>
</ikw:SimpleStackPanel>
</Border>
<!-- 工具切换 -->
<Border BorderThickness="1"
BorderBrush="{StaticResource BorderBrush}"
CornerRadius="8"
Padding="18,16"
Background="#27272a">
<ikw:SimpleStackPanel Spacing="10">
<TextBlock Text="工具切换"
FontSize="18"
FontWeight="SemiBold"
Foreground="{StaticResource TextForeground}" />
<Border Height="1"
Background="{StaticResource BorderBrush}"
HorizontalAlignment="Stretch" />
<ikw:SimpleStackPanel Spacing="4">
<local:HotkeyItem x:Name="SelectToolHotkey"
Title="选择工具"
Description="切换到选择工具"
DefaultKey="S"
DefaultModifiers="Alt" />
<local:HotkeyItem x:Name="DrawToolHotkey"
Title="绘图工具"
Description="切换到绘图工具"
DefaultKey="D"
DefaultModifiers="Alt" />
<local:HotkeyItem x:Name="EraserToolHotkey"
Title="橡皮擦工具"
Description="切换到橡皮擦工具"
DefaultKey="E"
DefaultModifiers="Alt" />
<local:HotkeyItem x:Name="BlackboardToolHotkey"
Title="黑板工具"
Description="切换到黑板工具"
DefaultKey="B"
DefaultModifiers="Alt" />
<local:HotkeyItem x:Name="QuitDrawToolHotkey"
Title="退出绘图/白板"
Description="退出绘图模式或白板模式"
DefaultKey="Q"
DefaultModifiers="Alt" />
</ikw:SimpleStackPanel>
</ikw:SimpleStackPanel>
</Border>
<!-- 画笔设置 -->
<Border BorderThickness="1"
BorderBrush="{StaticResource BorderBrush}"
CornerRadius="8"
Padding="18,16"
Background="#27272a">
<ikw:SimpleStackPanel Spacing="10">
<TextBlock Text="画笔设置"
FontSize="18"
FontWeight="SemiBold"
Foreground="{StaticResource TextForeground}" />
<Border Height="1"
Background="{StaticResource BorderBrush}"
HorizontalAlignment="Stretch" />
<ikw:SimpleStackPanel Spacing="4">
<local:HotkeyItem x:Name="Pen1Hotkey"
Title="画笔1"
Description="选择画笔1"
DefaultKey="D1"
DefaultModifiers="Alt" />
<local:HotkeyItem x:Name="Pen2Hotkey"
Title="画笔2"
Description="选择画笔2"
DefaultKey="D2"
DefaultModifiers="Alt" />
<local:HotkeyItem x:Name="Pen3Hotkey"
Title="画笔3"
Description="选择画笔3"
DefaultKey="D3"
DefaultModifiers="Alt" />
<local:HotkeyItem x:Name="Pen4Hotkey"
Title="画笔4"
Description="选择画笔4"
DefaultKey="D4"
DefaultModifiers="Alt" />
<local:HotkeyItem x:Name="Pen5Hotkey"
Title="画笔5"
Description="选择画笔5"
DefaultKey="D5"
DefaultModifiers="Alt" />
</ikw:SimpleStackPanel>
</ikw:SimpleStackPanel>
</Border>
<!-- 功能快捷键 -->
<Border BorderThickness="1"
BorderBrush="{StaticResource BorderBrush}"
CornerRadius="8"
Padding="18,16"
Background="#27272a">
<ikw:SimpleStackPanel Spacing="10">
<TextBlock Text="功能快捷键"
FontSize="18"
FontWeight="SemiBold"
Foreground="{StaticResource TextForeground}" />
<Border Height="1"
Background="{StaticResource BorderBrush}"
HorizontalAlignment="Stretch" />
<ikw:SimpleStackPanel Spacing="4">
<local:HotkeyItem x:Name="DrawLineHotkey"
Title="绘制直线"
Description="绘制直线工具"
DefaultKey="L"
DefaultModifiers="Alt" />
<local:HotkeyItem x:Name="ScreenshotHotkey"
Title="截图"
Description="保存屏幕截图到桌面"
DefaultKey="C"
DefaultModifiers="Alt" />
<local:HotkeyItem x:Name="QuickDrawHotkey"
Title="快抽"
Description="打开快抽窗口(与悬浮快抽按钮相同)"
DefaultKey="K"
DefaultModifiers="Alt" />
<local:HotkeyItem x:Name="HideHotkey"
Title="隐藏"
Description="隐藏应用程序"
DefaultKey="V"
DefaultModifiers="Alt" />
<local:HotkeyItem x:Name="ExitHotkey"
Title="退出"
Description="退出当前模式或应用程序"
DefaultKey="Escape"
DefaultModifiers="None" />
</ikw:SimpleStackPanel>
</ikw:SimpleStackPanel>
</Border>
</ikw:SimpleStackPanel>
</ikw:SimpleStackPanel>
</ui:ScrollViewerEx>
</Border>
<!-- 底部操作栏 -->
<Border Grid.Row="2"
Background="#18181b"
BorderBrush="{StaticResource BorderBrush}"
BorderThickness="0,1,0,0"
Padding="16,12">
<ikw:SimpleStackPanel Orientation="Horizontal"
HorizontalAlignment="Right"
VerticalAlignment="Center"
Spacing="10">
<Button x:Name="BtnResetToDefault"
Content="重置为默认"
MinWidth="100"
Height="36"
Style="{StaticResource SecondaryButtonStyle}"
Click="BtnResetToDefault_Click" />
<Button x:Name="BtnSave"
Content="保存设置"
MinWidth="100"
Height="36"
Style="{StaticResource PrimaryButtonStyle}"
Click="BtnSave_Click" />
</ikw:SimpleStackPanel>
</Border>
</Grid>
</Window>
@@ -1,628 +0,0 @@
using Ink_Canvas.Helpers;
using System;
using System.Collections.Generic;
using System.Reflection;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using MessageBox = iNKORE.UI.WPF.Modern.Controls.MessageBox;
namespace Ink_Canvas.Windows
{
/// <summary>
/// 快捷键设置窗口
/// </summary>
public partial class HotkeySettingsWindow : Window
{
#region Private Fields
private readonly MainWindow _mainWindow;
private readonly GlobalHotkeyManager _hotkeyManager;
private readonly Dictionary<string, HotkeyItem> _hotkeyItems;
#endregion
#region Constructor
public HotkeySettingsWindow(MainWindow mainWindow, GlobalHotkeyManager hotkeyManager)
{
InitializeComponent();
_mainWindow = mainWindow;
_hotkeyManager = hotkeyManager;
_hotkeyItems = new Dictionary<string, HotkeyItem>();
// 设置窗口属性
SetupWindowProperties();
InitializeHotkeyItems();
// 延迟加载快捷键,确保快捷键管理器已完全初始化
Loaded += (s, e) =>
{
try
{
// 不启用快捷键注册功能,只读取配置文件中的快捷键信息用于显示
// 这样用户可以看到配置文件中保存的快捷键,但不会自动注册
// 加载当前快捷键(包括配置文件中的)
LoadCurrentHotkeys();
SetupEventHandlers();
// 初始化鼠标模式快捷键设置
InitializeMouseModeSettings();
}
catch (Exception ex)
{
LogHelper.WriteLogToFile($"快捷键设置窗口初始化时出错: {ex.Message}", LogHelper.LogType.Error);
}
};
// 注册窗口关闭事件
Closed += HotkeySettingsWindow_Closed;
}
#endregion
#region Private Methods
/// <summary>
/// 设置窗口属性
/// </summary>
private void SetupWindowProperties()
{
try
{
// 设置窗口启动位置为屏幕中心
WindowStartupLocation = WindowStartupLocation.CenterScreen;
// 确保窗口在显示时获得焦点
ShowInTaskbar = true;
LogHelper.WriteLogToFile("快捷键设置窗口属性已设置");
}
catch (Exception ex)
{
LogHelper.WriteLogToFile($"设置快捷键设置窗口属性时出错: {ex.Message}", LogHelper.LogType.Error);
}
}
private void InitializeHotkeyItems()
{
try
{
LogHelper.WriteLogToFile("开始初始化快捷键项");
// 初始化快捷键项并设置HotkeyName
_hotkeyItems["Undo"] = UndoHotkey;
UndoHotkey.HotkeyName = "Undo";
_hotkeyItems["Redo"] = RedoHotkey;
RedoHotkey.HotkeyName = "Redo";
_hotkeyItems["Clear"] = ClearHotkey;
ClearHotkey.HotkeyName = "Clear";
_hotkeyItems["Paste"] = PasteHotkey;
PasteHotkey.HotkeyName = "Paste";
_hotkeyItems["SelectTool"] = SelectToolHotkey;
SelectToolHotkey.HotkeyName = "SelectTool";
_hotkeyItems["DrawTool"] = DrawToolHotkey;
DrawToolHotkey.HotkeyName = "DrawTool";
_hotkeyItems["EraserTool"] = EraserToolHotkey;
EraserToolHotkey.HotkeyName = "EraserTool";
_hotkeyItems["BlackboardTool"] = BlackboardToolHotkey;
BlackboardToolHotkey.HotkeyName = "BlackboardTool";
_hotkeyItems["QuitDrawTool"] = QuitDrawToolHotkey;
QuitDrawToolHotkey.HotkeyName = "QuitDrawTool";
_hotkeyItems["Pen1"] = Pen1Hotkey;
Pen1Hotkey.HotkeyName = "Pen1";
_hotkeyItems["Pen2"] = Pen2Hotkey;
Pen2Hotkey.HotkeyName = "Pen2";
_hotkeyItems["Pen3"] = Pen3Hotkey;
Pen3Hotkey.HotkeyName = "Pen3";
_hotkeyItems["Pen4"] = Pen4Hotkey;
Pen4Hotkey.HotkeyName = "Pen4";
_hotkeyItems["Pen5"] = Pen5Hotkey;
Pen5Hotkey.HotkeyName = "Pen5";
_hotkeyItems["DrawLine"] = DrawLineHotkey;
DrawLineHotkey.HotkeyName = "DrawLine";
_hotkeyItems["Screenshot"] = ScreenshotHotkey;
ScreenshotHotkey.HotkeyName = "Screenshot";
_hotkeyItems["QuickDraw"] = QuickDrawHotkey;
QuickDrawHotkey.HotkeyName = "QuickDraw";
_hotkeyItems["Hide"] = HideHotkey;
HideHotkey.HotkeyName = "Hide";
_hotkeyItems["Exit"] = ExitHotkey;
ExitHotkey.HotkeyName = "Exit";
LogHelper.WriteLogToFile($"成功初始化 {_hotkeyItems.Count} 个快捷键项");
}
catch (Exception ex)
{
LogHelper.WriteLogToFile($"初始化快捷键项时出错: {ex.Message}", LogHelper.LogType.Error);
}
}
private void LoadCurrentHotkeys()
{
try
{
// 首先尝试从配置文件获取快捷键信息
var configHotkeys = _hotkeyManager.GetHotkeysFromConfigFile();
LogHelper.WriteLogToFile($"配置文件中的快捷键数量: {configHotkeys.Count}");
// 显示配置文件中的快捷键
foreach (var hotkey in configHotkeys)
{
if (_hotkeyItems.TryGetValue(hotkey.Name, out var hotkeyItem))
{
hotkeyItem.SetCurrentHotkey(hotkey.Key, hotkey.Modifiers);
LogHelper.WriteLogToFile($"从配置文件设置快捷键项: {hotkey.Name} -> {hotkey.Modifiers}+{hotkey.Key}");
}
}
// 为没有快捷键的项目设置默认显示值(仅用于UI显示,不实际注册)
foreach (var kvp in _hotkeyItems)
{
var hotkeyItem = kvp.Value;
if (hotkeyItem.GetCurrentHotkey().key == Key.None)
{
// 根据DefaultKey和DefaultModifiers设置默认显示值
SetDefaultHotkeyForItem(hotkeyItem);
LogHelper.WriteLogToFile($"设置默认显示值: {hotkeyItem.HotkeyName}");
}
}
}
catch (Exception ex)
{
LogHelper.WriteLogToFile($"加载当前快捷键时出错: {ex.Message}", LogHelper.LogType.Error);
}
}
/// <summary>
/// 为快捷键项设置默认值
/// </summary>
private void SetDefaultHotkeyForItem(HotkeyItem hotkeyItem)
{
try
{
// 根据HotkeyName设置默认快捷键
switch (hotkeyItem.HotkeyName)
{
case "Undo":
hotkeyItem.SetCurrentHotkey(Key.Z, ModifierKeys.Control);
break;
case "Redo":
hotkeyItem.SetCurrentHotkey(Key.Y, ModifierKeys.Control);
break;
case "Clear":
hotkeyItem.SetCurrentHotkey(Key.E, ModifierKeys.Control);
break;
case "Paste":
hotkeyItem.SetCurrentHotkey(Key.V, ModifierKeys.Control);
break;
case "SelectTool":
hotkeyItem.SetCurrentHotkey(Key.S, ModifierKeys.Alt);
break;
case "DrawTool":
hotkeyItem.SetCurrentHotkey(Key.D, ModifierKeys.Alt);
break;
case "EraserTool":
hotkeyItem.SetCurrentHotkey(Key.E, ModifierKeys.Alt);
break;
case "BlackboardTool":
hotkeyItem.SetCurrentHotkey(Key.B, ModifierKeys.Alt);
break;
case "QuitDrawTool":
hotkeyItem.SetCurrentHotkey(Key.Q, ModifierKeys.Alt);
break;
case "Pen1":
hotkeyItem.SetCurrentHotkey(Key.D1, ModifierKeys.Alt);
break;
case "Pen2":
hotkeyItem.SetCurrentHotkey(Key.D2, ModifierKeys.Alt);
break;
case "Pen3":
hotkeyItem.SetCurrentHotkey(Key.D3, ModifierKeys.Alt);
break;
case "Pen4":
hotkeyItem.SetCurrentHotkey(Key.D4, ModifierKeys.Alt);
break;
case "Pen5":
hotkeyItem.SetCurrentHotkey(Key.D5, ModifierKeys.Alt);
break;
case "DrawLine":
hotkeyItem.SetCurrentHotkey(Key.L, ModifierKeys.Alt);
break;
case "Screenshot":
hotkeyItem.SetCurrentHotkey(Key.C, ModifierKeys.Alt);
break;
case "QuickDraw":
hotkeyItem.SetCurrentHotkey(Key.K, ModifierKeys.Alt);
break;
case "Hide":
hotkeyItem.SetCurrentHotkey(Key.V, ModifierKeys.Alt);
break;
case "Exit":
hotkeyItem.SetCurrentHotkey(Key.Escape, ModifierKeys.None);
break;
}
}
catch (Exception)
{
// 设置默认快捷键时出错,忽略
}
}
private void SetupEventHandlers()
{
// 为每个快捷键项设置事件处理器
foreach (var hotkeyItem in _hotkeyItems.Values)
{
hotkeyItem.HotkeyChanged += OnHotkeyChanged;
}
}
/// <summary>
/// 初始化鼠标模式快捷键设置
/// </summary>
private void InitializeMouseModeSettings()
{
try
{
// 设置开关的初始状态
ToggleSwitchEnableHotkeysInMouseMode.IsOn = MainWindow.Settings.Appearance.EnableHotkeysInMouseMode;
// 绑定开关变化事件
ToggleSwitchEnableHotkeysInMouseMode.Toggled += OnMouseModeHotkeyToggleChanged;
LogHelper.WriteLogToFile($"鼠标模式快捷键设置已初始化: {MainWindow.Settings.Appearance.EnableHotkeysInMouseMode}");
}
catch (Exception ex)
{
LogHelper.WriteLogToFile($"初始化鼠标模式快捷键设置时出错: {ex.Message}", LogHelper.LogType.Error);
}
}
/// <summary>
/// 鼠标模式快捷键开关变化事件处理
/// </summary>
private void OnMouseModeHotkeyToggleChanged(object sender, RoutedEventArgs e)
{
try
{
// 更新设置
MainWindow.Settings.Appearance.EnableHotkeysInMouseMode = ToggleSwitchEnableHotkeysInMouseMode.IsOn;
// 立即保存设置
MainWindow.SaveSettingsToFile();
// 如果快捷键管理器存在,立即更新快捷键状态
if (_hotkeyManager != null)
{
// 检查当前是否处于鼠标模式
bool isCurrentlyMouseMode = _mainWindow.inkCanvas.EditingMode == InkCanvasEditingMode.None;
// 如果当前处于鼠标模式且关闭了开关,立即禁用快捷键
if (isCurrentlyMouseMode && !ToggleSwitchEnableHotkeysInMouseMode.IsOn)
{
_hotkeyManager.DisableHotkeyRegistration();
LogHelper.WriteLogToFile("在鼠标模式下关闭快捷键开关,立即禁用快捷键");
}
else
{
// 其他情况正常更新快捷键状态
_hotkeyManager.UpdateHotkeyStateForToolMode(isCurrentlyMouseMode);
}
}
LogHelper.WriteLogToFile($"鼠标模式快捷键设置已更新: {MainWindow.Settings.Appearance.EnableHotkeysInMouseMode}", LogHelper.LogType.Event);
}
catch (Exception ex)
{
LogHelper.WriteLogToFile($"更新鼠标模式快捷键设置时出错: {ex.Message}", LogHelper.LogType.Error);
}
}
private void OnHotkeyChanged(object sender, HotkeyChangedEventArgs e)
{
try
{
LogHelper.WriteLogToFile($"收到快捷键变更事件: {e.HotkeyName} -> {e.Modifiers}+{e.Key}");
// 检查快捷键冲突
if (IsHotkeyConflict(e.Key, e.Modifiers, e.HotkeyName))
{
MessageBox.Show($"快捷键 {e.Modifiers}+{e.Key} 已被其他功能使用,请选择其他组合。",
"快捷键冲突", MessageBoxButton.OK, MessageBoxImage.Warning);
return;
}
// 更新快捷键管理器
UpdateHotkeyInManager(e.HotkeyName, e.Key, e.Modifiers);
}
catch (Exception ex)
{
LogHelper.WriteLogToFile($"处理快捷键变更时出错: {ex.Message}", LogHelper.LogType.Error);
}
}
private bool IsHotkeyConflict(Key key, ModifierKeys modifiers, string excludeHotkeyName)
{
// 检查是否与已注册的快捷键冲突
var registeredHotkeys = _hotkeyManager.GetRegisteredHotkeys();
foreach (var hotkey in registeredHotkeys)
{
if (hotkey.Name != excludeHotkeyName &&
hotkey.Key == key &&
hotkey.Modifiers == modifiers)
{
return true;
}
}
// 检查是否与默认快捷键冲突(如果当前快捷键项还没有注册)
if (excludeHotkeyName != null && _hotkeyItems.TryGetValue(excludeHotkeyName, out var currentItem))
{
var currentHotkey = currentItem.GetCurrentHotkey();
if (currentHotkey.key == Key.None)
{
// 如果当前项还没有快捷键,检查是否与其他默认快捷键冲突
foreach (var kvp in _hotkeyItems)
{
if (kvp.Key != excludeHotkeyName)
{
var item = kvp.Value;
var itemHotkey = item.GetCurrentHotkey();
if (itemHotkey.key == key && itemHotkey.modifiers == modifiers)
{
return true;
}
}
}
}
}
return false;
}
private void UpdateHotkeyInManager(string hotkeyName, Key key, ModifierKeys modifiers)
{
try
{
LogHelper.WriteLogToFile($"开始更新快捷键: {hotkeyName} -> {modifiers}+{key}");
// 先注销原有的快捷键(如果存在)
_hotkeyManager.UnregisterHotkey(hotkeyName);
LogHelper.WriteLogToFile($"已注销原有快捷键: {hotkeyName}");
// 根据快捷键名称获取对应的动作
var action = GetActionForHotkey(hotkeyName);
if (action != null)
{
LogHelper.WriteLogToFile($"找到快捷键动作: {hotkeyName}");
// 直接注册新的快捷键
if (_hotkeyManager.RegisterHotkey(hotkeyName, key, modifiers, action))
{
LogHelper.WriteLogToFile($"成功注册新快捷键: {hotkeyName} -> {modifiers}+{key}");
// 立即保存到配置文件
_hotkeyManager.SaveHotkeysToSettings();
LogHelper.WriteLogToFile("已保存快捷键配置");
// 更新UI显示
LoadCurrentHotkeys();
LogHelper.WriteLogToFile("已更新UI显示");
LogHelper.WriteLogToFile($"快捷键 {hotkeyName} 已更新为 {modifiers}+{key} 并保存", LogHelper.LogType.Event);
}
else
{
LogHelper.WriteLogToFile($"更新快捷键 {hotkeyName} 失败", LogHelper.LogType.Error);
}
}
else
{
LogHelper.WriteLogToFile($"未找到快捷键 {hotkeyName} 对应的动作", LogHelper.LogType.Warning);
}
}
catch (Exception ex)
{
LogHelper.WriteLogToFile($"更新快捷键管理器时出错: {ex.Message}", LogHelper.LogType.Error);
}
}
private Action GetActionForHotkey(string hotkeyName)
{
switch (hotkeyName)
{
case "Undo":
return () => _mainWindow.SymbolIconUndo_MouseUp(null, null);
case "Redo":
return () => _mainWindow.SymbolIconRedo_MouseUp(null, null);
case "Clear":
return () => _mainWindow.SymbolIconDelete_MouseUp(null, null);
case "Paste":
return () => _mainWindow.HandleGlobalPaste(null, null);
case "SelectTool":
return () => _mainWindow.SymbolIconSelect_MouseUp(null, null);
case "DrawTool":
return () => _mainWindow.PenIcon_Click(null, null);
case "EraserTool":
return () => _mainWindow.EraserIcon_Click(null, null);
case "BlackboardTool":
return () => _mainWindow.ImageBlackboard_MouseUp(null, null);
case "QuitDrawTool":
return () => _mainWindow.CursorIcon_Click(null, null);
case "Pen1":
return () => SwitchToPenType(0);
case "Pen2":
return () => SwitchToPenType(1);
case "Pen3":
return () => SwitchToPenType(2);
case "Pen4":
return () => SwitchToPenType(3);
case "Pen5":
return () => SwitchToPenType(4);
case "DrawLine":
return () => _mainWindow.BtnDrawLine_Click(null, null);
case "Screenshot":
return () => _mainWindow.SaveScreenShotToDesktop();
case "QuickDraw":
return () => _mainWindow.OpenQuickDrawFromHotkey();
case "Hide":
return () => _mainWindow.SymbolIconEmoji_MouseUp(null, null);
case "Exit":
return () => _mainWindow.KeyExit(null, null);
default:
return null;
}
}
/// <summary>
/// 切换到指定笔类型
/// </summary>
/// <param name="penTypeIndex">笔类型索引</param>
private void SwitchToPenType(int penTypeIndex)
{
try
{
// 通过反射访问主窗口的penType字段
var penTypeField = _mainWindow.GetType().GetField("penType",
BindingFlags.NonPublic | BindingFlags.Instance);
if (penTypeField != null)
{
penTypeField.SetValue(_mainWindow, penTypeIndex);
// 调用CheckPenTypeUIState方法更新UI状态
var checkPenTypeMethod = _mainWindow.GetType().GetMethod("CheckPenTypeUIState",
BindingFlags.NonPublic | BindingFlags.Instance);
if (checkPenTypeMethod != null)
{
checkPenTypeMethod.Invoke(_mainWindow, null);
}
}
}
catch (Exception ex)
{
LogHelper.WriteLogToFile($"切换到笔类型{penTypeIndex}时出错: {ex.Message}", LogHelper.LogType.Error);
}
}
#endregion
#region Window Event Handlers
/// <summary>
/// 窗口关闭事件处理
/// </summary>
private void HotkeySettingsWindow_Closed(object sender, EventArgs e)
{
try
{
LogHelper.WriteLogToFile("快捷键设置窗口已关闭");
}
catch (Exception ex)
{
LogHelper.WriteLogToFile($"快捷键设置窗口关闭时出错: {ex.Message}", LogHelper.LogType.Error);
}
}
#endregion
#region Event Handlers
/// <summary>
/// 标题栏拖拽事件
/// </summary>
private void TitleBar_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{
if (e.ClickCount == 2)
{
// 双击标题栏切换最大化状态
if (WindowState == WindowState.Maximized)
{
WindowState = WindowState.Normal;
}
else
{
WindowState = WindowState.Maximized;
}
}
else
{
// 拖拽窗口
DragMove();
}
}
private void BtnResetToDefault_Click(object sender, RoutedEventArgs e)
{
try
{
var result = MessageBox.Show("确定要重置所有快捷键为默认设置吗?",
"确认重置", MessageBoxButton.YesNo, MessageBoxImage.Question);
if (result == MessageBoxResult.Yes)
{
// 先注销所有现有快捷键
_hotkeyManager.UnregisterAllHotkeys();
// 重置为默认快捷键
_hotkeyManager.RegisterDefaultHotkeys();
// 立即保存到配置文件
_hotkeyManager.SaveHotkeysToSettings();
// 更新UI显示
LoadCurrentHotkeys();
MessageBox.Show("快捷键已重置为默认设置。", "重置完成", MessageBoxButton.OK, MessageBoxImage.Information);
}
}
catch (Exception ex)
{
LogHelper.WriteLogToFile($"重置快捷键时出错: {ex.Message}", LogHelper.LogType.Error);
MessageBox.Show($"重置快捷键时出错: {ex.Message}", "错误", MessageBoxButton.OK, MessageBoxImage.Error);
}
}
private void BtnSave_Click(object sender, RoutedEventArgs e)
{
try
{
// 保存快捷键配置
_hotkeyManager.SaveHotkeysToSettings();
MessageBox.Show("快捷键设置已保存。", "保存成功", MessageBoxButton.OK, MessageBoxImage.Information);
Close();
}
catch (Exception ex)
{
LogHelper.WriteLogToFile($"保存快捷键设置时出错: {ex.Message}", LogHelper.LogType.Error);
MessageBox.Show($"保存快捷键设置时出错: {ex.Message}", "错误", MessageBoxButton.OK, MessageBoxImage.Error);
}
}
#endregion
}
#region Hotkey Changed Event Args
/// <summary>
/// 快捷键变更事件参数
/// </summary>
public class HotkeyChangedEventArgs : EventArgs
{
public string HotkeyName { get; set; }
public Key Key { get; set; }
public ModifierKeys Modifiers { get; set; }
}
#endregion
}
@@ -99,6 +99,13 @@
<ui:FontIcon Icon="{x:Static ui:SegoeFluentIcons.Personalize}" />
</ui:SettingsCard.HeaderIcon>
</ui:SettingsCard>
<ui:SettingsCard Header="快捷键" Description="快捷键设置"
IsClickEnabled="True" Click="QuickNavCard_Click" Tag="HotkeyPage">
<ui:SettingsCard.HeaderIcon>
<ui:FontIcon Icon="{x:Static ui:SegoeFluentIcons.KeyboardStandard}" />
</ui:SettingsCard.HeaderIcon>
</ui:SettingsCard>
<TextBlock Style="{StaticResource SettingsSectionHeaderTextBlockStyle}"
Text="画板" />
@@ -0,0 +1,187 @@
<ui:Page x:Class="Ink_Canvas.Windows.SettingsViews.Pages.HotkeyPage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:local="clr-namespace:Ink_Canvas.Windows.SettingsViews.Pages"
xmlns:ui="http://schemas.inkore.net/lib/ui/wpf/modern"
xmlns:ikw="http://schemas.inkore.net/lib/ui/wpf"
xmlns:sys="clr-namespace:System;assembly=mscorlib"
xmlns:controls="clr-namespace:Ink_Canvas.Controls;assembly=InkCanvas.Controls"
xmlns:hk="clr-namespace:Ink_Canvas.Windows"
mc:Ignorable="d"
Title="快捷键">
<ScrollViewer PanningMode="VerticalFirst">
<Grid Margin="59,0,59,0">
<FrameworkElement.Resources>
<sys:Double x:Key="SettingsCardSpacing">4</sys:Double>
<Style x:Key="SettingsSectionHeaderTextBlockStyle"
BasedOn="{StaticResource BodyStrongTextBlockStyle}"
TargetType="TextBlock">
<Style.Setters>
<Setter Property="Margin" Value="1,30,0,6" />
</Style.Setters>
</Style>
</FrameworkElement.Resources>
<Grid>
<ikw:SimpleStackPanel MaxWidth="1000"
HorizontalAlignment="Stretch"
Spacing="{StaticResource SettingsCardSpacing}">
<ui:InfoBar IsClosable="False"
IsOpen="True"
Severity="Informational"
Title="说明"
Message="在这里可以自定义全局快捷键。全局快捷键在任何情况下都能生效,即使应用程序不在焦点状态。" />
<!-- 鼠标模式 -->
<TextBlock Style="{StaticResource SettingsSectionHeaderTextBlockStyle}"
Text="鼠标模式" />
<controls:LabeledSettingsCard x:Name="CardEnableHotkeysInMouseMode"
Header="在鼠标模式下启用快捷键"
Description="开启后,即使在鼠标模式下快捷键也会生效"
Icon="{x:Static ui:SegoeFluentIcons.KeyboardStandard}"
SwitchName="ToggleSwitchEnableHotkeysInMouseMode"
Toggled="ToggleSwitchEnableHotkeysInMouseMode_Toggled" />
<!-- 基本操作 -->
<TextBlock Style="{StaticResource SettingsSectionHeaderTextBlockStyle}"
Text="基本操作" />
<hk:HotkeyItem x:Name="UndoHotkey"
Title="撤销"
Description="撤销上一步操作"
DefaultKey="Z"
DefaultModifiers="Control" />
<hk:HotkeyItem x:Name="RedoHotkey"
Title="重做"
Description="重做上一步操作"
DefaultKey="Y"
DefaultModifiers="Control" />
<hk:HotkeyItem x:Name="ClearHotkey"
Title="清空"
Description="清空当前画板内容"
DefaultKey="E"
DefaultModifiers="Control" />
<hk:HotkeyItem x:Name="PasteHotkey"
Title="粘贴"
Description="粘贴剪贴板内容"
DefaultKey="V"
DefaultModifiers="Control" />
<!-- 工具切换 -->
<TextBlock Style="{StaticResource SettingsSectionHeaderTextBlockStyle}"
Text="工具切换" />
<hk:HotkeyItem x:Name="SelectToolHotkey"
Title="选择工具"
Description="切换到选择工具"
DefaultKey="S"
DefaultModifiers="Alt" />
<hk:HotkeyItem x:Name="DrawToolHotkey"
Title="绘图工具"
Description="切换到绘图工具"
DefaultKey="D"
DefaultModifiers="Alt" />
<hk:HotkeyItem x:Name="EraserToolHotkey"
Title="橡皮擦工具"
Description="切换到橡皮擦工具"
DefaultKey="E"
DefaultModifiers="Alt" />
<hk:HotkeyItem x:Name="BlackboardToolHotkey"
Title="黑板工具"
Description="切换到黑板工具"
DefaultKey="B"
DefaultModifiers="Alt" />
<hk:HotkeyItem x:Name="QuitDrawToolHotkey"
Title="退出绘图/白板"
Description="退出绘图模式或白板模式"
DefaultKey="Q"
DefaultModifiers="Alt" />
<!-- 画笔设置 -->
<TextBlock Style="{StaticResource SettingsSectionHeaderTextBlockStyle}"
Text="画笔设置" />
<hk:HotkeyItem x:Name="Pen1Hotkey"
Title="画笔1"
Description="选择画笔1"
DefaultKey="D1"
DefaultModifiers="Alt" />
<hk:HotkeyItem x:Name="Pen2Hotkey"
Title="画笔2"
Description="选择画笔2"
DefaultKey="D2"
DefaultModifiers="Alt" />
<hk:HotkeyItem x:Name="Pen3Hotkey"
Title="画笔3"
Description="选择画笔3"
DefaultKey="D3"
DefaultModifiers="Alt" />
<hk:HotkeyItem x:Name="Pen4Hotkey"
Title="画笔4"
Description="选择画笔4"
DefaultKey="D4"
DefaultModifiers="Alt" />
<hk:HotkeyItem x:Name="Pen5Hotkey"
Title="画笔5"
Description="选择画笔5"
DefaultKey="D5"
DefaultModifiers="Alt" />
<!-- 功能快捷键 -->
<TextBlock Style="{StaticResource SettingsSectionHeaderTextBlockStyle}"
Text="功能快捷键" />
<hk:HotkeyItem x:Name="DrawLineHotkey"
Title="绘制直线"
Description="绘制直线工具"
DefaultKey="L"
DefaultModifiers="Alt" />
<hk:HotkeyItem x:Name="ScreenshotHotkey"
Title="截图"
Description="保存屏幕截图到桌面"
DefaultKey="C"
DefaultModifiers="Alt" />
<hk:HotkeyItem x:Name="QuickDrawHotkey"
Title="快抽"
Description="打开快抽窗口(与悬浮快抽按钮相同)"
DefaultKey="K"
DefaultModifiers="Alt" />
<hk:HotkeyItem x:Name="HideHotkey"
Title="隐藏"
Description="隐藏应用程序"
DefaultKey="V"
DefaultModifiers="Alt" />
<hk:HotkeyItem x:Name="ExitHotkey"
Title="退出"
Description="退出当前模式或应用程序"
DefaultKey="Escape"
DefaultModifiers="None" />
<!-- 操作按钮 -->
<ikw:SimpleStackPanel Orientation="Horizontal"
HorizontalAlignment="Right"
Spacing="10"
Margin="0,20,0,0">
<Button x:Name="BtnResetToDefault"
Content="重置为默认"
MinWidth="120"
Click="BtnResetToDefault_Click" />
<Button x:Name="BtnSave"
Content="保存设置"
MinWidth="120"
Style="{StaticResource AccentButtonStyle}"
Click="BtnSave_Click" />
</ikw:SimpleStackPanel>
<Rectangle Height="48" />
</ikw:SimpleStackPanel>
</Grid>
</Grid>
</ScrollViewer>
</ui:Page>
@@ -0,0 +1,366 @@
using Ink_Canvas.Helpers;
using Ink_Canvas.Windows;
using Ink_Canvas.Windows.SettingsViews.Helpers;
using System;
using System.Collections.Generic;
using System.Reflection;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using MessageBox = iNKORE.UI.WPF.Modern.Controls.MessageBox;
using Page = iNKORE.UI.WPF.Modern.Controls.Page;
namespace Ink_Canvas.Windows.SettingsViews.Pages
{
public partial class HotkeyPage : Page
{
private bool _isLoaded;
private readonly Dictionary<string, HotkeyItem> _hotkeyItems = new Dictionary<string, HotkeyItem>();
private GlobalHotkeyManager _hotkeyManager;
private MainWindow _mainWindow;
public HotkeyPage()
{
InitializeComponent();
Loaded += HotkeyPage_Loaded;
Unloaded += HotkeyPage_Unloaded;
}
private void HotkeyPage_Loaded(object sender, RoutedEventArgs e)
{
try
{
_mainWindow = Application.Current.MainWindow as MainWindow;
_hotkeyManager = GetHotkeyManager(_mainWindow);
InitializeHotkeyItems();
LoadCurrentHotkeys();
SetupEventHandlers();
LoadMouseModeSetting();
_isLoaded = true;
}
catch (Exception ex)
{
LogHelper.WriteLogToFile($"快捷键页面初始化时出错: {ex.Message}", LogHelper.LogType.Error);
}
}
private void HotkeyPage_Unloaded(object sender, RoutedEventArgs e)
{
_isLoaded = false;
foreach (var item in _hotkeyItems.Values)
{
item.HotkeyChanged -= OnHotkeyChanged;
}
_hotkeyItems.Clear();
}
private static GlobalHotkeyManager GetHotkeyManager(MainWindow mw)
{
if (mw == null) return null;
var field = typeof(MainWindow).GetField("_globalHotkeyManager",
BindingFlags.NonPublic | BindingFlags.Instance);
return field?.GetValue(mw) as GlobalHotkeyManager;
}
private void InitializeHotkeyItems()
{
_hotkeyItems["Undo"] = UndoHotkey; UndoHotkey.HotkeyName = "Undo";
_hotkeyItems["Redo"] = RedoHotkey; RedoHotkey.HotkeyName = "Redo";
_hotkeyItems["Clear"] = ClearHotkey; ClearHotkey.HotkeyName = "Clear";
_hotkeyItems["Paste"] = PasteHotkey; PasteHotkey.HotkeyName = "Paste";
_hotkeyItems["SelectTool"] = SelectToolHotkey; SelectToolHotkey.HotkeyName = "SelectTool";
_hotkeyItems["DrawTool"] = DrawToolHotkey; DrawToolHotkey.HotkeyName = "DrawTool";
_hotkeyItems["EraserTool"] = EraserToolHotkey; EraserToolHotkey.HotkeyName = "EraserTool";
_hotkeyItems["BlackboardTool"] = BlackboardToolHotkey; BlackboardToolHotkey.HotkeyName = "BlackboardTool";
_hotkeyItems["QuitDrawTool"] = QuitDrawToolHotkey; QuitDrawToolHotkey.HotkeyName = "QuitDrawTool";
_hotkeyItems["Pen1"] = Pen1Hotkey; Pen1Hotkey.HotkeyName = "Pen1";
_hotkeyItems["Pen2"] = Pen2Hotkey; Pen2Hotkey.HotkeyName = "Pen2";
_hotkeyItems["Pen3"] = Pen3Hotkey; Pen3Hotkey.HotkeyName = "Pen3";
_hotkeyItems["Pen4"] = Pen4Hotkey; Pen4Hotkey.HotkeyName = "Pen4";
_hotkeyItems["Pen5"] = Pen5Hotkey; Pen5Hotkey.HotkeyName = "Pen5";
_hotkeyItems["DrawLine"] = DrawLineHotkey; DrawLineHotkey.HotkeyName = "DrawLine";
_hotkeyItems["Screenshot"] = ScreenshotHotkey; ScreenshotHotkey.HotkeyName = "Screenshot";
_hotkeyItems["QuickDraw"] = QuickDrawHotkey; QuickDrawHotkey.HotkeyName = "QuickDraw";
_hotkeyItems["Hide"] = HideHotkey; HideHotkey.HotkeyName = "Hide";
_hotkeyItems["Exit"] = ExitHotkey; ExitHotkey.HotkeyName = "Exit";
}
private void LoadCurrentHotkeys()
{
try
{
if (_hotkeyManager != null)
{
var configHotkeys = _hotkeyManager.GetHotkeysFromConfigFile();
foreach (var hotkey in configHotkeys)
{
if (_hotkeyItems.TryGetValue(hotkey.Name, out var hotkeyItem))
{
hotkeyItem.SetCurrentHotkey(hotkey.Key, hotkey.Modifiers);
}
}
}
foreach (var kvp in _hotkeyItems)
{
var hotkeyItem = kvp.Value;
if (hotkeyItem.GetCurrentHotkey().key == Key.None)
{
SetDefaultHotkeyForItem(hotkeyItem);
}
}
}
catch (Exception ex)
{
LogHelper.WriteLogToFile($"加载当前快捷键时出错: {ex.Message}", LogHelper.LogType.Error);
}
}
private static void SetDefaultHotkeyForItem(HotkeyItem hotkeyItem)
{
switch (hotkeyItem.HotkeyName)
{
case "Undo": hotkeyItem.SetCurrentHotkey(Key.Z, ModifierKeys.Control); break;
case "Redo": hotkeyItem.SetCurrentHotkey(Key.Y, ModifierKeys.Control); break;
case "Clear": hotkeyItem.SetCurrentHotkey(Key.E, ModifierKeys.Control); break;
case "Paste": hotkeyItem.SetCurrentHotkey(Key.V, ModifierKeys.Control); break;
case "SelectTool": hotkeyItem.SetCurrentHotkey(Key.S, ModifierKeys.Alt); break;
case "DrawTool": hotkeyItem.SetCurrentHotkey(Key.D, ModifierKeys.Alt); break;
case "EraserTool": hotkeyItem.SetCurrentHotkey(Key.E, ModifierKeys.Alt); break;
case "BlackboardTool": hotkeyItem.SetCurrentHotkey(Key.B, ModifierKeys.Alt); break;
case "QuitDrawTool": hotkeyItem.SetCurrentHotkey(Key.Q, ModifierKeys.Alt); break;
case "Pen1": hotkeyItem.SetCurrentHotkey(Key.D1, ModifierKeys.Alt); break;
case "Pen2": hotkeyItem.SetCurrentHotkey(Key.D2, ModifierKeys.Alt); break;
case "Pen3": hotkeyItem.SetCurrentHotkey(Key.D3, ModifierKeys.Alt); break;
case "Pen4": hotkeyItem.SetCurrentHotkey(Key.D4, ModifierKeys.Alt); break;
case "Pen5": hotkeyItem.SetCurrentHotkey(Key.D5, ModifierKeys.Alt); break;
case "DrawLine": hotkeyItem.SetCurrentHotkey(Key.L, ModifierKeys.Alt); break;
case "Screenshot": hotkeyItem.SetCurrentHotkey(Key.C, ModifierKeys.Alt); break;
case "QuickDraw": hotkeyItem.SetCurrentHotkey(Key.K, ModifierKeys.Alt); break;
case "Hide": hotkeyItem.SetCurrentHotkey(Key.V, ModifierKeys.Alt); break;
case "Exit": hotkeyItem.SetCurrentHotkey(Key.Escape, ModifierKeys.None); break;
}
}
private void SetupEventHandlers()
{
foreach (var hotkeyItem in _hotkeyItems.Values)
{
hotkeyItem.HotkeyChanged += OnHotkeyChanged;
}
}
private void LoadMouseModeSetting()
{
CardEnableHotkeysInMouseMode.IsOn = SettingsManager.Settings.Appearance.EnableHotkeysInMouseMode;
}
private void ToggleSwitchEnableHotkeysInMouseMode_Toggled(object sender, RoutedEventArgs e)
{
if (!_isLoaded) return;
try
{
bool newState = CardEnableHotkeysInMouseMode.IsOn;
SettingsManager.Settings.Appearance.EnableHotkeysInMouseMode = newState;
SettingsManager.SaveSettingsToFile();
if (_hotkeyManager != null && _mainWindow != null)
{
bool isCurrentlyMouseMode = _mainWindow.inkCanvas.EditingMode == InkCanvasEditingMode.None;
if (isCurrentlyMouseMode && !newState)
{
_hotkeyManager.DisableHotkeyRegistration();
}
else
{
_hotkeyManager.UpdateHotkeyStateForToolMode(isCurrentlyMouseMode);
}
}
}
catch (Exception ex)
{
LogHelper.WriteLogToFile($"更新鼠标模式快捷键设置时出错: {ex.Message}", LogHelper.LogType.Error);
}
}
private void OnHotkeyChanged(object sender, HotkeyChangedEventArgs e)
{
try
{
if (_hotkeyManager == null)
{
MessageBox.Show("快捷键管理器尚未初始化,无法保存变更。", "错误",
MessageBoxButton.OK, MessageBoxImage.Error);
return;
}
if (IsHotkeyConflict(e.Key, e.Modifiers, e.HotkeyName))
{
MessageBox.Show($"快捷键 {e.Modifiers}+{e.Key} 已被其他功能使用,请选择其他组合。",
"快捷键冲突", MessageBoxButton.OK, MessageBoxImage.Warning);
return;
}
UpdateHotkeyInManager(e.HotkeyName, e.Key, e.Modifiers);
}
catch (Exception ex)
{
LogHelper.WriteLogToFile($"处理快捷键变更时出错: {ex.Message}", LogHelper.LogType.Error);
}
}
private bool IsHotkeyConflict(Key key, ModifierKeys modifiers, string excludeHotkeyName)
{
var registeredHotkeys = _hotkeyManager.GetRegisteredHotkeys();
foreach (var hotkey in registeredHotkeys)
{
if (hotkey.Name != excludeHotkeyName &&
hotkey.Key == key &&
hotkey.Modifiers == modifiers)
{
return true;
}
}
if (excludeHotkeyName != null && _hotkeyItems.TryGetValue(excludeHotkeyName, out var currentItem))
{
var currentHotkey = currentItem.GetCurrentHotkey();
if (currentHotkey.key == Key.None)
{
foreach (var kvp in _hotkeyItems)
{
if (kvp.Key != excludeHotkeyName)
{
var itemHotkey = kvp.Value.GetCurrentHotkey();
if (itemHotkey.key == key && itemHotkey.modifiers == modifiers)
{
return true;
}
}
}
}
}
return false;
}
private void UpdateHotkeyInManager(string hotkeyName, Key key, ModifierKeys modifiers)
{
try
{
_hotkeyManager.UnregisterHotkey(hotkeyName);
var action = GetActionForHotkey(hotkeyName);
if (action == null)
{
LogHelper.WriteLogToFile($"未找到快捷键 {hotkeyName} 对应的动作", LogHelper.LogType.Warning);
return;
}
if (_hotkeyManager.RegisterHotkey(hotkeyName, key, modifiers, action))
{
_hotkeyManager.SaveHotkeysToSettings();
LoadCurrentHotkeys();
LogHelper.WriteLogToFile($"快捷键 {hotkeyName} 已更新为 {modifiers}+{key} 并保存", LogHelper.LogType.Event);
}
}
catch (Exception ex)
{
LogHelper.WriteLogToFile($"更新快捷键管理器时出错: {ex.Message}", LogHelper.LogType.Error);
}
}
private Action GetActionForHotkey(string hotkeyName)
{
if (_mainWindow == null) return null;
switch (hotkeyName)
{
case "Undo": return () => _mainWindow.SymbolIconUndo_MouseUp(null, null);
case "Redo": return () => _mainWindow.SymbolIconRedo_MouseUp(null, null);
case "Clear": return () => _mainWindow.SymbolIconDelete_MouseUp(null, null);
case "Paste": return () => _mainWindow.HandleGlobalPaste(null, null);
case "SelectTool": return () => _mainWindow.SymbolIconSelect_MouseUp(null, null);
case "DrawTool": return () => _mainWindow.PenIcon_Click(null, null);
case "EraserTool": return () => _mainWindow.EraserIcon_Click(null, null);
case "BlackboardTool": return () => _mainWindow.ImageBlackboard_MouseUp(null, null);
case "QuitDrawTool": return () => _mainWindow.CursorIcon_Click(null, null);
case "Pen1": return () => SwitchToPenType(0);
case "Pen2": return () => SwitchToPenType(1);
case "Pen3": return () => SwitchToPenType(2);
case "Pen4": return () => SwitchToPenType(3);
case "Pen5": return () => SwitchToPenType(4);
case "DrawLine": return () => _mainWindow.BtnDrawLine_Click(null, null);
case "Screenshot": return () => _mainWindow.SaveScreenShotToDesktop();
case "QuickDraw": return () => _mainWindow.OpenQuickDrawFromHotkey();
case "Hide": return () => _mainWindow.SymbolIconEmoji_MouseUp(null, null);
case "Exit": return () => _mainWindow.KeyExit(null, null);
default: return null;
}
}
private void SwitchToPenType(int penTypeIndex)
{
try
{
if (_mainWindow == null) return;
var penTypeField = _mainWindow.GetType().GetField("penType",
BindingFlags.NonPublic | BindingFlags.Instance);
if (penTypeField != null)
{
penTypeField.SetValue(_mainWindow, penTypeIndex);
var checkPenTypeMethod = _mainWindow.GetType().GetMethod("CheckPenTypeUIState",
BindingFlags.NonPublic | BindingFlags.Instance);
checkPenTypeMethod?.Invoke(_mainWindow, null);
}
}
catch (Exception ex)
{
LogHelper.WriteLogToFile($"切换到笔类型{penTypeIndex}时出错: {ex.Message}", LogHelper.LogType.Error);
}
}
private void BtnResetToDefault_Click(object sender, RoutedEventArgs e)
{
try
{
if (_hotkeyManager == null) return;
var result = MessageBox.Show("确定要重置所有快捷键为默认设置吗?", "确认重置",
MessageBoxButton.YesNo, MessageBoxImage.Question);
if (result != MessageBoxResult.Yes) return;
_hotkeyManager.UnregisterAllHotkeys();
_hotkeyManager.RegisterDefaultHotkeys();
_hotkeyManager.SaveHotkeysToSettings();
LoadCurrentHotkeys();
MessageBox.Show("快捷键已重置为默认设置。", "重置完成",
MessageBoxButton.OK, MessageBoxImage.Information);
}
catch (Exception ex)
{
LogHelper.WriteLogToFile($"重置快捷键时出错: {ex.Message}", LogHelper.LogType.Error);
MessageBox.Show($"重置快捷键时出错: {ex.Message}", "错误",
MessageBoxButton.OK, MessageBoxImage.Error);
}
}
private void BtnSave_Click(object sender, RoutedEventArgs e)
{
try
{
if (_hotkeyManager == null) return;
_hotkeyManager.SaveHotkeysToSettings();
MessageBox.Show("快捷键设置已保存。", "保存成功",
MessageBoxButton.OK, MessageBoxImage.Information);
}
catch (Exception ex)
{
LogHelper.WriteLogToFile($"保存快捷键设置时出错: {ex.Message}", LogHelper.LogType.Error);
MessageBox.Show($"保存快捷键设置时出错: {ex.Message}", "错误",
MessageBoxButton.OK, MessageBoxImage.Error);
}
}
}
}
@@ -193,6 +193,15 @@
<ui:FontIcon Icon="{x:Static ui:SegoeFluentIcons.Personalize}"/>
</ui:NavigationViewItem.Icon>
</ui:NavigationViewItem>
<ui:NavigationViewItem
x:Name="HotkeyPageItem"
Content="快捷键"
Tag="HotkeyPage"
ToolTipService.ToolTip="快捷键设置">
<ui:NavigationViewItem.Icon>
<ui:FontIcon Icon="{x:Static ui:SegoeFluentIcons.KeyboardStandard}"/>
</ui:NavigationViewItem.Icon>
</ui:NavigationViewItem>
</ui:NavigationViewItem.MenuItems>
</ui:NavigationViewItem>
@@ -43,6 +43,7 @@ namespace Ink_Canvas.Windows.SettingsViews
{ "SecurityPage", typeof(SecurityPage) },
{ "WindowPage", typeof(WindowPage) },
{ "AppearancePage", typeof(AppearancePage) },
{ "HotkeyPage", typeof(HotkeyPage) },
{ "UpdatePage", typeof(UpdatePage) },
{ "ExperimentalPage", typeof(ExperimentalPage) },
{ "AdvancedPage", typeof(AdvancedPage) },