fix(shortcutguide): separate hotkey and Win hold activation (#50000)

## Summary of the Pull Request

Fixes a regression where `Win+Shift+/` was treated as a Windows-key hold
because both activation paths signaled the same event. Regular
activation now opens the full Shortcut Guide independently of the **Hold
Windows key** setting and remains visible after Win is released.

## PR Checklist

- [x] Closes: #49990
- [ ] **Communication:** Core contributor review is still required
- [x] **Tests:** Added/updated and all pass
- [x] **Localization:** N/A - no end-user-facing strings changed
- [x] **Dev docs:** N/A - no developer-facing contract or workflow
changed

## Detailed Description of the Pull Request / Additional comments

- Keeps regular activation on `OnHotkeyEx()` and routes Win-only holds
through the existing `on_hotkey(size_t)` module seam with a reserved ID.
- Adds a dedicated named event for Win-key hold activation while
preserving the existing regular trigger event and module ABI layout.
- Removes `GetAsyncKeyState` trigger-source inference from Shortcut
Guide UI.
- Tracks the source and visible surface explicitly so Win release closes
only hold-owned UI:
- regular hotkey always opens the full guide and is unaffected by hold
settings;
  - **Off** ignores Win-only holds;
  - **Taskbar indicators** closes on Win release;
- **Open Shortcut Guide** follows the configured close-on-release value.
- Handles both left and right Windows keys.
- Clears stale held-key registrations and pending timers before
re-registering them.
- Uses `MOD_NOREPEAT` for centralized activation hotkeys so a held chord
cannot repeatedly toggle the overlay.
- Normalizes `MOD_NOREPEAT` before centralized action lookup, validates
queued hold activations against the current Win-key state, and transfers
hold-owned full-guide surfaces to regular-hotkey ownership.
- Adds a pure activation policy and a data-driven unit-test matrix.

Touched areas:

- `src/runner/` - source-specific dispatch, held-key registration
cleanup, and repeat suppression.
- `src/common/interop/` - additive hold-event constant and WinRT
projection.
- `src/modules/ShortcutGuide/ShortcutGuideModuleInterface/` - dedicated
hold event signaling and hold-setting guard.
- `src/modules/ShortcutGuide/ShortcutGuide.Ui/` - explicit activation
routing and source-aware release behavior.
- `src/modules/ShortcutGuide/ShortcutGuide.UnitTests/` - activation and
release policy coverage.

**Risks and mitigations**

- The existing regular event and settings JSON remain unchanged.
- No virtual method or data member was added to `PowertoyModuleIface`;
the existing `on_hotkey(size_t)` method is reused.
- Duplicate hold callbacks are prevented during settings refresh, and
duplicate hold events are UI no-ops.
- No new telemetry or user-content logging was added.

## Validation Steps Performed

- Built the full x64 Release solution and confirmed the complete payload
starts without missing-module dialogs or Runner startup errors.
- After rebasing onto current `main`, reran
`tools\build\build-essentials.cmd -Platform x64 -Configuration Release`
and built the affected interop, Runner, module-interface,
index-generator, UI, and unit-test projects; all error logs were empty.
- Ran the x64 Release `ShortcutGuide.UnitTests.dll` with
`vstest.console.exe`: **43/43 passed**, including all 15
activation-policy cases.
- After addressing review feedback, rebuilt the x64 Release Runner,
Shortcut Guide UI, and unit-test projects; all builds passed and the
unit tests remained **43/43**.
- Signaled a queued hold after Win was released and confirmed no overlay
opened. Then exercised hold-owned full-guide to regular-hotkey ownership
transfer and confirmed both activations resolved to `ShowFullGuide` and
the guide remained visible after Win release.
- Held an injected `Win+Shift+/` chord for 1.4 seconds over Notepad: the
full guide opened, remained visible after Win release, and logs recorded
two regular activations (open/close) with **zero** hold activations.
- Triggered the dedicated hold event over Notepad in both hold modes:
- **Taskbar indicators:** the 2048x1104 overlay was visible and
`WS_EX_TOPMOST`.
- **Open Shortcut Guide:** the full guide and taskbar indicators were
visible in the same topmost overlay.

Manual verification matrix for a preview build:

1. Set **Hold Windows key** to Off, Taskbar indicators, and Open
Shortcut Guide.
2. In each mode, press and release `Win+Shift+/`; confirm one full panel
remains visible.
3. Hold LWin and RWin separately; confirm only the selected hold
behavior runs.
4. For Open Shortcut Guide, verify close-on-release enabled and
disabled.
5. Verify a second regular activation toggles the full panel closed.

For physical Win-hold checks, run PowerToys at the same or higher
integrity level as the foreground app. `RegisterHotKey` activation can
work across an elevation mismatch while Runner's low-level hold hook
cannot observe the key.

Closes #49990

---------

Copilot-Session: 8271bded-18e8-474e-8e3b-addd71f67f50
This commit is contained in:
Boliang Zhang
2026-08-19 16:11:11 +08:00
committed by GitHub
parent 5eeb979339
commit ab1f521067
14 changed files with 545 additions and 122 deletions

View File

@@ -41,6 +41,12 @@ public:
Logger::warn(L"Failed to create {} event. {}", CommonSharedConstants::SHORTCUT_GUIDE_TRIGGER_EVENT, get_last_error_or_default(GetLastError()));
}
winKeyHoldEvent = CreateEvent(nullptr, false, false, CommonSharedConstants::SHORTCUT_GUIDE_WIN_KEY_HOLD_EVENT);
if (!winKeyHoldEvent)
{
Logger::warn(L"Failed to create {} event. {}", CommonSharedConstants::SHORTCUT_GUIDE_WIN_KEY_HOLD_EVENT, get_last_error_or_default(GetLastError()));
}
InitSettings();
}
@@ -132,6 +138,10 @@ public:
{
CloseHandle(triggerEvent);
}
if (winKeyHoldEvent)
{
CloseHandle(winKeyHoldEvent);
}
delete this;
}
@@ -144,18 +154,17 @@ public:
virtual void OnHotkeyEx() override
{
Logger::trace("OnHotkeyEx()");
if (!_enabled)
SignalEvent(triggerEvent, CommonSharedConstants::SHORTCUT_GUIDE_TRIGGER_EVENT, L"regular hotkey");
}
virtual bool on_hotkey(size_t hotkeyId) override
{
if (hotkeyId == PowertoyModuleIface::WIN_KEY_HOLD_HOTKEY_ID && m_windowsKeyAction != WindowsKeyAction::Off)
{
return;
SignalEvent(winKeyHoldEvent, CommonSharedConstants::SHORTCUT_GUIDE_WIN_KEY_HOLD_EVENT, L"Windows key hold");
}
if (!IsProcessActive())
{
StartProcess();
}
SetEvent(triggerEvent);
return false;
}
virtual void send_settings_telemetry() override
@@ -170,6 +179,13 @@ public:
virtual UINT milliseconds_win_key_must_be_pressed() override { return m_millisecondsWinKeyPressTimeForGlobalWindowsShortcuts; }
private:
enum class WindowsKeyAction
{
Off = 0,
TaskbarIndicators = 1,
OpenShortcutGuide = 2,
};
std::wstring app_name;
//contains the non localized key of the powertoy
std::wstring app_key;
@@ -186,7 +202,28 @@ private:
UINT m_millisecondsWinKeyPressTimeForTaskbarIconShortcuts = DEFAULT_MILLISECONDS_WIN_KEY_PRESS_TIME_FOR_TASKBAR_ICON_SHORTCUTS;
HANDLE triggerEvent;
HANDLE winKeyHoldEvent;
HANDLE exitEvent;
WindowsKeyAction m_windowsKeyAction = WindowsKeyAction::TaskbarIndicators;
void SignalEvent(HANDLE eventHandle, const wchar_t* eventName, const wchar_t* activationSource)
{
Logger::trace(L"Shortcut Guide was invoked by {}", activationSource);
if (!_enabled)
{
return;
}
if (!IsProcessActive() && !StartProcess())
{
return;
}
if (!SetEvent(eventHandle))
{
Logger::error(L"Failed to signal {}. {}", eventName, get_last_error_or_default(GetLastError()));
}
}
bool StartProcess(std::wstring args = L"")
{
@@ -199,6 +236,10 @@ private:
{
ResetEvent(triggerEvent);
}
if (winKeyHoldEvent)
{
ResetEvent(winKeyHoldEvent);
}
unsigned long powertoys_pid = GetCurrentProcessId();
std::wstring executable_args = L"";
@@ -302,7 +343,7 @@ private:
Logger::warn("Failed to initialize Shortcut Guide start shortcut");
}
try
try
{
auto propertiesObject = settingsObject.GetNamedObject(L"properties");
if (propertiesObject.HasKey(L"press_time"))
@@ -324,7 +365,34 @@ try
}
}
}
catch (...) { /* Keep defaults */ }
catch (...)
{ /* Keep defaults */
}
try
{
auto propertiesObject = settingsObject.GetNamedObject(L"properties");
if (propertiesObject.HasKey(L"win_key_action"))
{
const auto value = static_cast<int>(propertiesObject.GetNamedObject(L"win_key_action").GetNamedNumber(L"value"));
switch (value)
{
case static_cast<int>(WindowsKeyAction::Off):
m_windowsKeyAction = WindowsKeyAction::Off;
break;
case static_cast<int>(WindowsKeyAction::OpenShortcutGuide):
m_windowsKeyAction = WindowsKeyAction::OpenShortcutGuide;
break;
case static_cast<int>(WindowsKeyAction::TaskbarIndicators):
default:
m_windowsKeyAction = WindowsKeyAction::TaskbarIndicators;
break;
}
}
}
catch (...)
{ /* Keep defaults */
}
}
else
{