基于WebView2实现的Win11桌面框架,利用插件自定义
chunhualin1
2026-08-17 9b5711d3124622b1f1b18500d442c76492da877a
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
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();
    }
}