Add hotkey shortcuts to Command Palette context menu items (#40359)

## Summary

Adds keyboard shortcuts to Command Palette context menu items to match
PowerToys Run functionality, providing users with faster access to
context actions without needing to open the context menu.

## Changes Made

Added `RequestedShortcut` properties to context menu items in both
`UWPApplication.cs` and `Win32Program.cs`:

### Keyboard Shortcuts Implemented

**UWP Applications:**
- Run as Admin: `Ctrl+Shift+Enter`
- Copy Path: `Ctrl+Shift+P`
- Open Containing Folder: `Ctrl+Shift+E`
- Open in Console: `Ctrl+Shift+C`

**Win32 Programs:**
- Run as Admin: `Ctrl+Shift+Enter`
- Run as Different User: `Ctrl+Shift+U`
- Copy Path: `Ctrl+Shift+P`
- Open Containing Folder: `Ctrl+Shift+E`
- Open in Console: `Ctrl+Shift+C`

## Implementation Details

- Added `using Windows.System;` import to access `VirtualKey` enum
- Used `KeyChordHelpers.FromModifiers()` to create keyboard shortcuts
- Applied shortcuts to `CommandContextItem` objects in `GetCommands()`
methods
- Maintained all existing functionality while adding hotkey
accessibility

### Code Example

```csharp
commands.Add(new CommandContextItem(
    new RunAsAdminCommand(path, directory, false))
{
    RequestedShortcut = KeyChordHelpers.FromModifiers(ctrl: true, shift: true, vkey: VirtualKey.Enter),
});
```

## User Experience

Users can now:
- Select an app in Command Palette search results
- Press hotkeys directly (e.g., `Ctrl+Shift+E` to open containing
folder)
- Access context actions without opening the context menu (`Ctrl+K`)
- Enjoy the same hotkey experience as PowerToys Run

This makes Command Palette faster and more consistent with PowerToys
Run, addressing the user request for "having a possibility to directly
trigger any of those options with hotkey from the search results."

Fixes #40358.

<!-- START COPILOT CODING AGENT TIPS -->
---

💬 Share your feedback on Copilot coding agent for the chance to win a
$200 gift card! Click
[here](https://survey.alchemer.com/s3/8343779/Copilot-Coding-agent) to
start the survey.

---------

Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com>
Co-authored-by: zadjii-msft <18356694+zadjii-msft@users.noreply.github.com>
This commit is contained in:
Copilot
2025-07-09 21:45:27 -05:00
committed by GitHub
parent d944b8728c
commit 7a1c27dcf3
2 changed files with 38 additions and 9 deletions

View File

@@ -12,6 +12,7 @@ using Microsoft.CmdPal.Ext.Apps.Commands;
using Microsoft.CmdPal.Ext.Apps.Properties;
using Microsoft.CmdPal.Ext.Apps.Utils;
using Microsoft.CommandPalette.Extensions.Toolkit;
using Windows.System;
using Windows.Win32;
using Windows.Win32.Storage.Packaging.Appx;
using PackageVersion = Microsoft.CmdPal.Ext.Apps.Programs.UWP.PackageVersion;
@@ -77,14 +78,20 @@ public class UWPApplication : IProgram
{
commands.Add(
new CommandContextItem(
new RunAsAdminCommand(UniqueIdentifier, string.Empty, true)));
new RunAsAdminCommand(UniqueIdentifier, string.Empty, true))
{
RequestedShortcut = KeyChordHelpers.FromModifiers(ctrl: true, shift: true, vkey: VirtualKey.Enter),
});
// We don't add context menu to 'run as different user', because UWP applications normally installed per user and not for all users.
}
commands.Add(
new CommandContextItem(
new CopyPathCommand(Location)));
new CopyPathCommand(Location))
{
RequestedShortcut = KeyChordHelpers.FromModifiers(ctrl: true, shift: true, vkey: VirtualKey.C),
});
commands.Add(
new CommandContextItem(
@@ -92,11 +99,17 @@ public class UWPApplication : IProgram
{
Name = Resources.open_containing_folder,
Icon = new("\ue838"),
}));
})
{
RequestedShortcut = KeyChordHelpers.FromModifiers(ctrl: true, shift: true, vkey: VirtualKey.E),
});
commands.Add(
new CommandContextItem(
new OpenInConsoleCommand(Package.Location)));
new OpenInConsoleCommand(Package.Location))
{
RequestedShortcut = KeyChordHelpers.FromModifiers(ctrl: true, shift: true, vkey: VirtualKey.R),
});
return commands;
}

View File

@@ -22,6 +22,7 @@ using Microsoft.CmdPal.Ext.Apps.Properties;
using Microsoft.CmdPal.Ext.Apps.Utils;
using Microsoft.CommandPalette.Extensions.Toolkit;
using Microsoft.Win32;
using Windows.System;
namespace Microsoft.CmdPal.Ext.Apps.Programs;
@@ -192,20 +193,35 @@ public class Win32Program : IProgram
if (AppType != ApplicationType.InternetShortcutApplication && AppType != ApplicationType.Folder && AppType != ApplicationType.GenericFile)
{
commands.Add(new CommandContextItem(
new RunAsAdminCommand(!string.IsNullOrEmpty(LnkFilePath) ? LnkFilePath : FullPath, ParentDirectory, false)));
new RunAsAdminCommand(!string.IsNullOrEmpty(LnkFilePath) ? LnkFilePath : FullPath, ParentDirectory, false))
{
RequestedShortcut = KeyChordHelpers.FromModifiers(ctrl: true, shift: true, vkey: VirtualKey.Enter),
});
commands.Add(new CommandContextItem(
new RunAsUserCommand(!string.IsNullOrEmpty(LnkFilePath) ? LnkFilePath : FullPath, ParentDirectory)));
new RunAsUserCommand(!string.IsNullOrEmpty(LnkFilePath) ? LnkFilePath : FullPath, ParentDirectory))
{
RequestedShortcut = KeyChordHelpers.FromModifiers(ctrl: true, shift: true, vkey: VirtualKey.U),
});
}
commands.Add(new CommandContextItem(
new CopyPathCommand(FullPath)));
new CopyPathCommand(FullPath))
{
RequestedShortcut = KeyChordHelpers.FromModifiers(ctrl: true, shift: true, vkey: VirtualKey.C),
});
commands.Add(new CommandContextItem(
new OpenPathCommand(ParentDirectory)));
new OpenPathCommand(ParentDirectory))
{
RequestedShortcut = KeyChordHelpers.FromModifiers(ctrl: true, shift: true, vkey: VirtualKey.E),
});
commands.Add(new CommandContextItem(
new OpenInConsoleCommand(ParentDirectory)));
new OpenInConsoleCommand(ParentDirectory))
{
RequestedShortcut = KeyChordHelpers.FromModifiers(ctrl: true, shift: true, vkey: VirtualKey.R),
});
return commands;
}