2026-05-02 10:06:21 +08:00
|
|
|
|
using Ink_Canvas.Helpers;
|
2025-08-23 21:39:00 +08:00
|
|
|
|
using System;
|
|
|
|
|
|
using System.Windows;
|
|
|
|
|
|
using System.Windows.Controls;
|
|
|
|
|
|
using System.Windows.Input;
|
|
|
|
|
|
|
|
|
|
|
|
namespace Ink_Canvas.Windows
|
|
|
|
|
|
{
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 快捷键项控件
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
public partial class HotkeyItem : UserControl
|
|
|
|
|
|
{
|
2026-05-01 17:36:53 +08:00
|
|
|
|
public static readonly DependencyProperty TitleProperty =
|
|
|
|
|
|
DependencyProperty.Register(nameof(Title), typeof(string), typeof(HotkeyItem),
|
|
|
|
|
|
new PropertyMetadata(string.Empty));
|
2026-04-05 22:03:16 +08:00
|
|
|
|
|
2026-05-01 17:36:53 +08:00
|
|
|
|
public static readonly DependencyProperty DescriptionProperty =
|
|
|
|
|
|
DependencyProperty.Register(nameof(Description), typeof(string), typeof(HotkeyItem),
|
|
|
|
|
|
new PropertyMetadata(string.Empty));
|
2025-08-23 21:39:00 +08:00
|
|
|
|
|
|
|
|
|
|
public string Title
|
|
|
|
|
|
{
|
2026-05-01 17:36:53 +08:00
|
|
|
|
get => (string)GetValue(TitleProperty);
|
|
|
|
|
|
set => SetValue(TitleProperty, value);
|
2025-08-23 21:39:00 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public string Description
|
|
|
|
|
|
{
|
2026-05-01 17:36:53 +08:00
|
|
|
|
get => (string)GetValue(DescriptionProperty);
|
|
|
|
|
|
set => SetValue(DescriptionProperty, value);
|
2025-08-23 21:39:00 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public string DefaultKey { get; set; }
|
|
|
|
|
|
public string DefaultModifiers { get; set; }
|
2025-08-31 11:43:52 +08:00
|
|
|
|
|
2025-08-24 02:27:57 +08:00
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 快捷键名称(用于标识,如"Undo")
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
public string HotkeyName { get; set; }
|
2025-08-23 21:39:00 +08:00
|
|
|
|
|
2026-05-01 17:36:53 +08:00
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 快捷键变更事件
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
public event EventHandler<HotkeyChangedEventArgs> HotkeyChanged;
|
|
|
|
|
|
|
2025-08-23 21:39:00 +08:00
|
|
|
|
private Key _currentKey = Key.None;
|
|
|
|
|
|
private ModifierKeys _currentModifiers = ModifierKeys.None;
|
|
|
|
|
|
|
|
|
|
|
|
public HotkeyItem()
|
|
|
|
|
|
{
|
|
|
|
|
|
InitializeComponent();
|
|
|
|
|
|
UpdateHotkeyDisplay();
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public void SetCurrentHotkey(Key key, ModifierKeys modifiers)
|
|
|
|
|
|
{
|
|
|
|
|
|
_currentKey = key;
|
|
|
|
|
|
_currentModifiers = modifiers;
|
|
|
|
|
|
UpdateHotkeyDisplay();
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public (Key key, ModifierKeys modifiers) GetCurrentHotkey()
|
|
|
|
|
|
{
|
|
|
|
|
|
return (_currentKey, _currentModifiers);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
private void UpdateHotkeyDisplay()
|
|
|
|
|
|
{
|
|
|
|
|
|
if (_currentKey == Key.None)
|
|
|
|
|
|
{
|
2026-05-02 10:06:21 +08:00
|
|
|
|
CurrentHotkeyTextBlock.Text = LocalizationHelper.GetString("Hotkey_NotSet");
|
2025-08-23 21:39:00 +08:00
|
|
|
|
}
|
|
|
|
|
|
else
|
|
|
|
|
|
{
|
|
|
|
|
|
var modifiersText = _currentModifiers == ModifierKeys.None ? "" : $"{_currentModifiers}+";
|
|
|
|
|
|
CurrentHotkeyTextBlock.Text = $"{modifiersText}{_currentKey}";
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2026-05-01 17:43:29 +08:00
|
|
|
|
private static HotkeyItem _activeCaptureItem;
|
|
|
|
|
|
|
2025-08-23 21:39:00 +08:00
|
|
|
|
private void StartHotkeyCapture()
|
|
|
|
|
|
{
|
2026-05-01 17:43:29 +08:00
|
|
|
|
if (_activeCaptureItem != null && _activeCaptureItem != this)
|
|
|
|
|
|
{
|
|
|
|
|
|
_activeCaptureItem.StopHotkeyCapture();
|
|
|
|
|
|
}
|
|
|
|
|
|
_activeCaptureItem = this;
|
|
|
|
|
|
|
2026-05-01 17:39:27 +08:00
|
|
|
|
CurrentHotkeyTextBlock.Text = "请按键...";
|
2025-08-23 21:39:00 +08:00
|
|
|
|
Focus();
|
|
|
|
|
|
KeyDown += HotkeyItem_KeyDown;
|
|
|
|
|
|
KeyUp += HotkeyItem_KeyUp;
|
2026-05-01 17:43:29 +08:00
|
|
|
|
LostFocus += HotkeyItem_LostFocus;
|
2025-08-23 21:39:00 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
private void StopHotkeyCapture()
|
|
|
|
|
|
{
|
2026-05-01 17:39:27 +08:00
|
|
|
|
UpdateHotkeyDisplay();
|
2025-08-23 21:39:00 +08:00
|
|
|
|
KeyDown -= HotkeyItem_KeyDown;
|
|
|
|
|
|
KeyUp -= HotkeyItem_KeyUp;
|
2026-05-01 17:43:29 +08:00
|
|
|
|
LostFocus -= HotkeyItem_LostFocus;
|
|
|
|
|
|
if (_activeCaptureItem == this)
|
|
|
|
|
|
{
|
|
|
|
|
|
_activeCaptureItem = null;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
private void HotkeyItem_LostFocus(object sender, RoutedEventArgs e)
|
|
|
|
|
|
{
|
|
|
|
|
|
StopHotkeyCapture();
|
2025-08-23 21:39:00 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
private void HotkeyItem_KeyDown(object sender, KeyEventArgs e)
|
|
|
|
|
|
{
|
|
|
|
|
|
e.Handled = true;
|
2025-08-31 11:43:52 +08:00
|
|
|
|
|
2025-08-23 21:39:00 +08:00
|
|
|
|
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 ||
|
|
|
|
|
|
e.Key == Key.LWin || e.Key == Key.RWin)
|
|
|
|
|
|
{
|
|
|
|
|
|
return;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
var modifiers = ModifierKeys.None;
|
|
|
|
|
|
if (Keyboard.IsKeyDown(Key.LeftCtrl) || Keyboard.IsKeyDown(Key.RightCtrl))
|
|
|
|
|
|
modifiers |= ModifierKeys.Control;
|
|
|
|
|
|
if (Keyboard.IsKeyDown(Key.LeftShift) || Keyboard.IsKeyDown(Key.RightShift))
|
|
|
|
|
|
modifiers |= ModifierKeys.Shift;
|
|
|
|
|
|
if (Keyboard.IsKeyDown(Key.LeftAlt) || Keyboard.IsKeyDown(Key.RightAlt))
|
|
|
|
|
|
modifiers |= ModifierKeys.Alt;
|
|
|
|
|
|
if (Keyboard.IsKeyDown(Key.LWin) || Keyboard.IsKeyDown(Key.RWin))
|
|
|
|
|
|
modifiers |= ModifierKeys.Windows;
|
|
|
|
|
|
|
|
|
|
|
|
_currentKey = e.Key;
|
|
|
|
|
|
_currentModifiers = modifiers;
|
2025-08-31 11:43:52 +08:00
|
|
|
|
|
2025-08-23 21:39:00 +08:00
|
|
|
|
UpdateHotkeyDisplay();
|
|
|
|
|
|
StopHotkeyCapture();
|
|
|
|
|
|
|
|
|
|
|
|
HotkeyChanged?.Invoke(this, new HotkeyChangedEventArgs
|
|
|
|
|
|
{
|
2026-05-01 17:36:53 +08:00
|
|
|
|
HotkeyName = HotkeyName ?? Title,
|
2025-08-23 21:39:00 +08:00
|
|
|
|
Key = _currentKey,
|
|
|
|
|
|
Modifiers = _currentModifiers
|
|
|
|
|
|
});
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
private void HotkeyItem_KeyUp(object sender, KeyEventArgs e)
|
|
|
|
|
|
{
|
|
|
|
|
|
e.Handled = true;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
private void BtnSetHotkey_Click(object sender, RoutedEventArgs e)
|
|
|
|
|
|
{
|
|
|
|
|
|
StartHotkeyCapture();
|
|
|
|
|
|
}
|
2026-05-01 17:36:53 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 快捷键变更事件参数
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
public class HotkeyChangedEventArgs : EventArgs
|
|
|
|
|
|
{
|
|
|
|
|
|
public string HotkeyName { get; set; }
|
|
|
|
|
|
public Key Key { get; set; }
|
|
|
|
|
|
public ModifierKeys Modifiers { get; set; }
|
2025-08-23 21:39:00 +08:00
|
|
|
|
}
|
2025-08-31 11:43:52 +08:00
|
|
|
|
}
|