Skip to content

Commit 361a64a

Browse files
committed
Add ShortcutHotkey struct to better manage the hotkey experience.
1 parent 90f4ccb commit 361a64a

4 files changed

Lines changed: 411 additions & 5 deletions

File tree

src/PSADT/PSADT.Interop/HOTKEYF.cs

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
using System;
2+
3+
namespace PSADT.Interop
4+
{
5+
/// <summary>
6+
/// Defines modifier key flags used to specify keyboard shortcuts (hotkeys) in the Windows API.
7+
/// </summary>
8+
/// <remarks>This enumeration allows combining multiple modifier keys, such as SHIFT, CONTROL, and ALT,
9+
/// using bitwise operations to register complex hotkey combinations. The values correspond to those defined by the
10+
/// Win32 API, ensuring compatibility when interacting with native Windows functionality.</remarks>
11+
[System.Diagnostics.CodeAnalysis.SuppressMessage("Naming", "CA1712:Do not prefix enum values with type name", Justification = "This is as they're defined within the Win32 API.")]
12+
[Flags]
13+
internal enum HOTKEYF : uint
14+
{
15+
/// <summary>
16+
/// Represents the SHIFT key modifier for hotkeys.
17+
/// </summary>
18+
HOTKEYF_SHIFT = Windows.Win32.PInvoke.HOTKEYF_SHIFT,
19+
20+
/// <summary>
21+
/// Represents the CONTROL key modifier for hotkeys.
22+
/// </summary>
23+
HOTKEYF_CONTROL = Windows.Win32.PInvoke.HOTKEYF_CONTROL,
24+
25+
/// <summary>
26+
/// Represents the ALT key modifier for hotkeys.
27+
/// </summary>
28+
HOTKEYF_ALT = Windows.Win32.PInvoke.HOTKEYF_ALT,
29+
30+
/// <summary>
31+
/// Represents the extended hotkey flag used in the Windows API.
32+
/// </summary>
33+
HOTKEYF_EXT = Windows.Win32.PInvoke.HOTKEYF_EXT,
34+
}
35+
}

src/PSADT/PSADT.Interop/NativeMethods.txt

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,10 @@ HKEY_CURRENT_CONFIG
7474
HKEY_CURRENT_USER
7575
HKEY_LOCAL_MACHINE
7676
HKEY_USERS
77+
HOTKEYF_ALT
78+
HOTKEYF_CONTROL
79+
HOTKEYF_EXT
80+
HOTKEYF_SHIFT
7781
HRESULT_FROM_WIN32
7882
HTBORDER
7983
HTBOTTOM
@@ -2132,6 +2136,7 @@ VerLanguageName
21322136
VerQueryValue
21332137
VirtualAlloc
21342138
VirtualFree
2139+
VIRTUAL_KEY
21352140
VS_FIXEDFILEINFO
21362141
WaitForSingleObject
21372142
Windows.Win32.UI.Controls.IImageList

src/PSADT/PSADT/ShortcutManagement/ShellLinkFile.cs

Lines changed: 23 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -312,22 +312,40 @@ public string? Arguments
312312
/// Gets or sets the hotkey for the shortcut.
313313
/// </summary>
314314
/// <value>
315-
/// The hotkey as a 16-bit value where the low-order byte is the virtual key code
316-
/// and the high-order byte contains modifier flags.
315+
/// The hotkey combination as a string (e.g., "Ctrl+Shift+Alt+Q").
316+
/// Returns <see langword="null"/> if no hotkey is assigned.
317317
/// </value>
318+
/// <exception cref="ArgumentException">Thrown when the hotkey string format is invalid.</exception>
318319
/// <exception cref="COMException">Thrown when the COM operation fails.</exception>
319-
public ushort? Hotkey
320+
/// <example>
321+
/// <code>
322+
/// // Set hotkey using WScript.Shell-compatible string format
323+
/// shortcut.Hotkey = "ALT+CTRL+F";
324+
/// shortcut.Hotkey = "Ctrl+Shift+Q";
325+
///
326+
/// // Read and display hotkey
327+
/// Console.WriteLine(shortcut.Hotkey); // Output: "Ctrl+Shift+Q"
328+
/// </code>
329+
/// </example>
330+
public string? Hotkey
320331
{
321332
get
322333
{
323334
ObjectDisposedException.ThrowIf(_disposed, this);
324335
_shellLink.GetHotkey(out ushort hotkey);
325-
return hotkey > 0 ? hotkey : null;
336+
return hotkey > 0 ? ShortcutHotkey.FromValue(hotkey).ToString() : null;
326337
}
327338
set
328339
{
329340
ObjectDisposedException.ThrowIf(_disposed, this);
330-
_shellLink.SetHotkey(value ?? 0);
341+
if (string.IsNullOrWhiteSpace(value))
342+
{
343+
_shellLink.SetHotkey(0);
344+
}
345+
else
346+
{
347+
_shellLink.SetHotkey(ShortcutHotkey.Parse(value!).Value);
348+
}
331349
}
332350
}
333351

0 commit comments

Comments
 (0)