From 09d4e2f1a1bc206c80d8d3287d070c70ec7fd63d Mon Sep 17 00:00:00 2001
From: chunhualin1 <chunhualin1@lenovo.com>
Date: 星期五, 14 八月 2026 20:00:52 +0800
Subject: [PATCH] Merge branch 'master' of http://git.linmoli.cn/r/Moli.Desktop
---
src/Moli.Desktop/Hotkeys/HotkeyChord.cs | 189 +++++++++++++++++++++++++++++++++++++++++++++++
1 files changed, 189 insertions(+), 0 deletions(-)
diff --git a/src/Moli.Desktop/Hotkeys/HotkeyChord.cs b/src/Moli.Desktop/Hotkeys/HotkeyChord.cs
new file mode 100644
index 0000000..75c0b45
--- /dev/null
+++ b/src/Moli.Desktop/Hotkeys/HotkeyChord.cs
@@ -0,0 +1,189 @@
+using System.Globalization;
+using System.Text;
+using Moli.Desktop.Runtime;
+
+namespace Moli.Desktop.Hotkeys;
+
+public readonly record struct HotkeyChord(uint Modifiers, uint VirtualKey, string Canonical)
+{
+ public const uint ModAlt = 0x0001;
+ public const uint ModControl = 0x0002;
+ public const uint ModShift = 0x0004;
+ public const uint ModWin = 0x0008;
+ public const uint ModNoRepeat = 0x4000;
+
+ public uint RegisterModifiers => Modifiers | ModNoRepeat;
+
+ public static bool TryParse(string? keys, out HotkeyChord chord, out string? errorCode)
+ {
+ chord = default;
+ errorCode = null;
+ if (string.IsNullOrWhiteSpace(keys))
+ {
+ errorCode = "HotkeyInvalidKeys";
+ return false;
+ }
+
+ var parts = keys.Split('+', StringSplitOptions.TrimEntries | StringSplitOptions.RemoveEmptyEntries);
+ if (parts.Length == 0)
+ {
+ errorCode = "HotkeyInvalidKeys";
+ return false;
+ }
+
+ uint modifiers = 0;
+ string? keyToken = null;
+ foreach (var part in parts)
+ {
+ if (TryParseModifier(part, out var modifier))
+ {
+ modifiers |= modifier;
+ continue;
+ }
+
+ if (keyToken is not null)
+ {
+ errorCode = "HotkeyInvalidKeys";
+ return false;
+ }
+
+ keyToken = part;
+ }
+
+ if (keyToken is null)
+ {
+ errorCode = "HotkeyModifierOnly";
+ return false;
+ }
+
+ if (!TryParseVirtualKey(keyToken, out var virtualKey, out var canonicalKey))
+ {
+ errorCode = "HotkeyInvalidKeys";
+ return false;
+ }
+
+ if (modifiers == 0)
+ {
+ errorCode = "HotkeyInvalidKeys";
+ return false;
+ }
+
+ chord = new HotkeyChord(modifiers, virtualKey, FormatCanonical(modifiers, canonicalKey));
+ return true;
+ }
+
+ public static OperationResult ValidateKeys(string? keys)
+ => TryParse(keys, out _, out var errorCode)
+ ? OperationResult.Success()
+ : OperationResult.Failure(errorCode ?? "HotkeyInvalidKeys", $"Shortcut '{keys}' is invalid.");
+
+ private static bool TryParseModifier(string token, out uint modifier)
+ {
+ modifier = token.ToLowerInvariant() switch
+ {
+ "ctrl" or "control" => ModControl,
+ "alt" => ModAlt,
+ "shift" => ModShift,
+ "win" or "windows" or "meta" => ModWin,
+ _ => 0
+ };
+ return modifier != 0;
+ }
+
+ private static bool TryParseVirtualKey(string token, out uint virtualKey, out string canonicalKey)
+ {
+ virtualKey = 0;
+ canonicalKey = token;
+ if (token.Length == 1)
+ {
+ var ch = char.ToUpperInvariant(token[0]);
+ if (ch is >= 'A' and <= 'Z')
+ {
+ virtualKey = ch;
+ canonicalKey = ch.ToString();
+ return true;
+ }
+
+ if (ch is >= '0' and <= '9')
+ {
+ virtualKey = ch;
+ canonicalKey = ch.ToString();
+ return true;
+ }
+ }
+
+ if (token.StartsWith("F", StringComparison.OrdinalIgnoreCase)
+ && int.TryParse(token[1..], NumberStyles.Integer, CultureInfo.InvariantCulture, out var function)
+ && function is >= 1 and <= 24)
+ {
+ virtualKey = (uint)(0x6F + function);
+ canonicalKey = "F" + function.ToString(CultureInfo.InvariantCulture);
+ return true;
+ }
+
+ switch (token.ToLowerInvariant())
+ {
+ case "space":
+ virtualKey = 0x20;
+ canonicalKey = "Space";
+ return true;
+ case "tab":
+ virtualKey = 0x09;
+ canonicalKey = "Tab";
+ return true;
+ case "esc" or "escape":
+ virtualKey = 0x1B;
+ canonicalKey = "Esc";
+ return true;
+ case "enter" or "return":
+ virtualKey = 0x0D;
+ canonicalKey = "Enter";
+ return true;
+ case "up":
+ virtualKey = 0x26;
+ canonicalKey = "Up";
+ return true;
+ case "down":
+ virtualKey = 0x28;
+ canonicalKey = "Down";
+ return true;
+ case "left":
+ virtualKey = 0x25;
+ canonicalKey = "Left";
+ return true;
+ case "right":
+ virtualKey = 0x27;
+ canonicalKey = "Right";
+ return true;
+ default:
+ return false;
+ }
+ }
+
+ private static string FormatCanonical(uint modifiers, string key)
+ {
+ var builder = new StringBuilder();
+ if ((modifiers & ModControl) != 0)
+ {
+ builder.Append("Ctrl+");
+ }
+
+ if ((modifiers & ModAlt) != 0)
+ {
+ builder.Append("Alt+");
+ }
+
+ if ((modifiers & ModShift) != 0)
+ {
+ builder.Append("Shift+");
+ }
+
+ if ((modifiers & ModWin) != 0)
+ {
+ builder.Append("Win+");
+ }
+
+ builder.Append(key);
+ return builder.ToString();
+ }
+}
--
Gitblit v1.9.1