Skip to content

[KBM] New editor: key dropdown for empty slots, scope recording to the editor - #51150

Merged
Yu Leng (moooyo) merged 5 commits into
mainfrom
niels9001/kbm-editor-empty-key-recording
Oct 14, 2026
Merged

Yu Leng (moooyo) merged 5 commits into
mainfrom
niels9001/kbm-editor-empty-key-recording

Conversation

@niels9001

@niels9001 Niels Laute (niels9001) commented Oct 9, 2026 •

Copy link
Copy Markdown
Collaborator

Summary of the Pull Request

This PR fixes two related input gaps in the new WinUI 3 Keyboard Manager editor compared with the classic editor.

  1. Keys that can't be recorded have no fallback. When a mapping starts empty, the trigger and action showed only the "press keys" recording area. Keys you can't press (media keys, or keys missing from your keyboard) or that the hook can't capture could not be chosen. The classic editor always showed a dropdown. An empty mapping now shows a caption hint ("Press to start recording keys or") followed by a "Select key" KeyDropDownButton, so any key can be picked from the list. The dropdown's flyout has a search box at the top that filters the key list. Picking a modifier adds another empty dropdown, so a full shortcut can be built with the mouse.
  2. Recording leaked and trapped focus. The low-level recording hook stayed active even when the editor wasn't in the foreground, so keystrokes typed in other apps got recorded. While recording, the editor also swallowed Tab, so keyboard users couldn't leave the field. Now:
    • The hook only records when the editor is in the foreground.
    • Tab (or Shift+Tab) with no other keys held stops recording and moves focus forward (or backward). Keys you already recorded are kept; the previous keys come back only if nothing was recorded.
    • The recording toggle unchecks when recording stops.
Image

PR Checklist

Detailed Description of the Pull Request / Additional comments

  • KeyDropDownButton:
    • Shows a localized "Select key" placeholder (KeyDropDownButton_SelectKeyPlaceholder) when KeyName is empty.
    • The flyout now has an AutoSuggestBox ("Search keys") above the ListView. It gets focus when the flyout opens and filters by display name, ignoring case. Enter picks the first match, and "No keys found" shows when nothing matches. The search text resets each time the flyout opens.
  • UnifiedMappingControl:
    • New TriggerEmptyKeyPickerPanel and ActionEmptyKeyPickerPanel appear when the trigger or action has no keys. Each shows a CaptionTextBlockStyle hint ("Press to start recording keys or") and an empty KeyDropDownButton.
    • Picking a key from the dropdown fills the first slot (TrySetFirstDropDownKey).
    • Before recording starts, the previous keys are saved. If recording ends with nothing captured, they are restored.
    • Key getters ignore empty placeholder entries, so an empty slot is never saved as a key.
  • KeyboardHookHelper:
    • The hook's "is active" predicate is now IsEditorForeground: the foreground window's process must be the editor's own process.
    • ShouldHandleKeyboardEvent treats Tab or Shift+Tab with no other keys held as "exit recording". It restores the previous keys only if nothing was captured (or only the Shift held for Shift+Tab). For Shift+Tab it swallows the Tab, because the hook already swallowed Shift and the window would see a plain Tab, and the control moves focus backward with FocusManager.TryMoveFocus scoped to the dialog.
    • Cleanup is deferred to the DispatcherQueue because the native KeyboardHook::HookProc is still running the callback, and disposing the hook from inside it is unsafe.
    • CleanupHook tells the target through the new IKeyboardHookTarget.OnRecordingStopped(bool), so the toggle unchecks and keys are restored when needed.

Trade-offs:

  • A lone Tab can no longer be recorded. You can still pick it from the dropdown, which this PR makes always available.

Part of the new-vs-classic KBM editor parity work. Other draft PRs from the same work: #51147 (language), #51148 (engine suspend) and #51149 (orphaned-key warning).

Validation Steps Performed

  • tools\build\build.cmd in src\modules\keyboardmanager\KeyboardManagerEditorUI (arm64 Debug): passed, exit code 0.
  • No new unit tests: the changes are in UI and hook plumbing, which the existing unit test project doesn't cover.
  • Manually tested on an arm64 Debug dev runner: new mappings show the caption hint and "Select key" for both trigger and action. The search box gets focus when the flyout opens, and typing "k" filters the list. Typing in the search box doesn't start key recording.
  • Other manual checks:
    • Add a new mapping and confirm the empty trigger and action show "Select key". Pick a media key (e.g. Play/Pause) from the dropdown and save.
    • Start recording, switch to another app and type there. Confirm nothing is recorded in the editor.
    • Start recording and press Tab. Confirm recording stops, the toggle unchecks and focus moves on. Record a key, release it, then press Tab: the recorded key stays. Press Shift+Tab: focus moves backward.
    • Start recording and stop without pressing a key. Confirm the previous keys come back.
…e editor

Show a 'Select key' dropdown for empty trigger/action keys so unrecordable keys (e.g. media keys) can be picked; only record while the editor is foreground; Tab exits recording.

Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 Changes recommended

Tab currently discards captured input, Shift+Tab navigates incorrectly, and the new picker lacks automated coverage.

3 open findings
What changed in this PR

Adds empty-slot key selection and scopes keyboard recording to the foreground editor.

Changes:

  • Adds localized key-picker placeholders for empty mappings.
  • Stops recording on Tab and editor deactivation.
  • Restores pre-recording keys when recording is cancelled.
File Description
Resources.resw Adds localized “Select key” text.
KeyboardHookHelper.cs Adds foreground filtering and recording-exit handling.
UnifiedMappingControl.xaml.cs Integrates empty pickers and recording cleanup.
UnifiedMappingControl.xaml Adds empty trigger/action key pickers.
KeyDropDownButton.xaml.cs Switches between key and placeholder visuals.
KeyDropDownButton.xaml Renders the localized empty state.

🧠 Review effort: Balanced


💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.

Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
@niels9001
Niels Laute (niels9001) marked this pull request as ready for review October 11, 2026 15:31
@github-actions github-actions Bot added the Needs-Author-Feedback The original author of the issue/PR needs to come back and respond to something label Oct 11, 2026
@github-actions

github-actions Bot commented Oct 11, 2026 •

Copy link
Copy Markdown

✅ PR intake

All automated intake checks now pass. Thanks for the updates!

Automated PR intake; PowerToys maintainers make final decisions.

@github-actions github-actions Bot added Ready for review and removed Needs-Author-Feedback The original author of the issue/PR needs to come back and respond to something labels Oct 11, 2026
…+Tab

Addresses review feedback: Tab only restores the previous keys when nothing was recorded, and Shift+Tab now moves focus backward instead of forward.

Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>

@moooyo Yu Leng (moooyo) left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reviewed 4671e537039fdeb185190d93e0157ffed954ddca using a local x64 Debug build. The build and all 26 EditorUI unit tests passed. Focused keyboard/UI runs reproduced the four interaction issues below and the existing UI test's selection mismatch. An explicit search/list-focus path successfully saved and reloaded 1 -> 2; the full KeyboardManager.UITests fixture was not run locally. The two suggested code replacements are proposed fixes and still need build/UI validation after application.

…ing exit

- Move focus backward with XamlRoot.Content as the search root (dialog roots reject Previous)
- Only exit recording on Tab key-down; expose key state via a new HotkeySettingsControlHook overload
- Submit the key search from the query text and let Escape close the picker from the search box
- Update the keyboard selection UI test and cover picking media keys from empty slots

Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
Copilot-Session: 4c7f42fd-7fad-43c2-9281-e581ddfd45eb

@moooyo Yu Leng (moooyo) left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM now

@moooyo
Yu Leng (moooyo) merged commit 9501b98 into main Oct 14, 2026
13 checks passed
@moooyo
Yu Leng (moooyo) deleted the niels9001/kbm-editor-empty-key-recording branch October 14, 2026 05:19
Boliang Zhang (LegendaryBlair) pushed a commit that referenced this pull request Oct 15, 2026
…e editor (#51150)

<!-- Enter a brief description/summary of your PR here. What does it
fix/what does it change/how was it tested (even manually, if necessary)?
-->
## Summary of the Pull Request

This PR fixes two related input gaps in the new WinUI 3 Keyboard Manager
editor compared with the classic editor.

1. **Keys that can't be recorded have no fallback.** When a mapping
starts empty, the trigger and action showed only the "press keys"
recording area. Keys you can't press (media keys, or keys missing from
your keyboard) or that the hook can't capture could not be chosen. The
classic editor always showed a dropdown. An empty mapping now shows a
caption hint ("Press to start recording keys or") followed by a "Select
key" `KeyDropDownButton`, so any key can be picked from the list. The
dropdown's flyout has a search box at the top that filters the key list.
Picking a modifier adds another empty dropdown, so a full shortcut can
be built with the mouse.
2. **Recording leaked and trapped focus.** The low-level recording hook
stayed active even when the editor wasn't in the foreground, so
keystrokes typed in other apps got recorded. While recording, the editor
also swallowed Tab, so keyboard users couldn't leave the field. Now:
   - The hook only records when the editor is in the foreground.
- Tab (or Shift+Tab) with no other keys held stops recording and moves
focus forward (or backward). Keys you already recorded are kept; the
previous keys come back only if nothing was recorded.
   - The recording toggle unchecks when recording stops.

<img width="880" height="724" alt="Image"
src="https://github.com/user-attachments/assets/aa266b16-43d8-4ab5-a582-e5dc8a18c590"
/>

<!-- Please review the items on the PR checklist before submitting-->
## PR Checklist

- [x] Closes: #50852
- [x] Closes: #50360
- [x] Closes: #49158
- [ ] **Communication:** I've discussed this with core contributors
already. If the work hasn't been agreed, this work might be rejected
- [ ] **Tests:** Added/updated and all pass
- [x] **Localization:** All end-user-facing strings can be localized
- [ ] **Dev docs:** Added/updated
- [ ] **New binaries:** Added on the required places
- [ ] [JSON for
signing](https://github.com/microsoft/PowerToys/blob/main/.pipelines/ESRPSigning_core.json)
for new binaries
- [ ] [WXS for
installer](https://github.com/microsoft/PowerToys/blob/main/installer/PowerToysSetup/Product.wxs)
for new binaries and localization folder
- [ ] [YML for CI
pipeline](https://github.com/microsoft/PowerToys/blob/main/.pipelines/ci/templates/build-powertoys-steps.yml)
for new test projects
- [ ] [YML for signed
pipeline](https://github.com/microsoft/PowerToys/blob/main/.pipelines/release.yml)
- [ ] **Documentation updated:** If checked, please file a pull request
on [our docs
repo](https://github.com/MicrosoftDocs/windows-uwp/tree/docs/hub/powertoys)
and link it here: #xxx

<!-- Provide a more detailed description of the PR, other things fixed,
or any additional comments/features here -->
## Detailed Description of the Pull Request / Additional comments

- **`KeyDropDownButton`**:
- Shows a localized "Select key" placeholder
(`KeyDropDownButton_SelectKeyPlaceholder`) when `KeyName` is empty.
- The flyout now has an `AutoSuggestBox` ("Search keys") above the
`ListView`. It gets focus when the flyout opens and filters by display
name, ignoring case. Enter picks the first match, and "No keys found"
shows when nothing matches. The search text resets each time the flyout
opens.
- **`UnifiedMappingControl`**:
- New `TriggerEmptyKeyPickerPanel` and `ActionEmptyKeyPickerPanel`
appear when the trigger or action has no keys. Each shows a
`CaptionTextBlockStyle` hint ("Press to start recording keys or") and an
empty `KeyDropDownButton`.
- Picking a key from the dropdown fills the first slot
(`TrySetFirstDropDownKey`).
- Before recording starts, the previous keys are saved. If recording
ends with nothing captured, they are restored.
- Key getters ignore empty placeholder entries, so an empty slot is
never saved as a key.
- **`KeyboardHookHelper`**:
- The hook's "is active" predicate is now `IsEditorForeground`: the
foreground window's process must be the editor's own process.
- `ShouldHandleKeyboardEvent` treats Tab or Shift+Tab with no other keys
held as "exit recording". It restores the previous keys only if nothing
was captured (or only the Shift held for Shift+Tab). For Shift+Tab it
swallows the Tab, because the hook already swallowed Shift and the
window would see a plain Tab, and the control moves focus backward with
`FocusManager.TryMoveFocus` scoped to the dialog.
- Cleanup is deferred to the `DispatcherQueue` because the native
`KeyboardHook::HookProc` is still running the callback, and disposing
the hook from inside it is unsafe.
- `CleanupHook` tells the target through the new
`IKeyboardHookTarget.OnRecordingStopped(bool)`, so the toggle unchecks
and keys are restored when needed.

**Trade-offs:**
- A lone Tab can no longer be recorded. You can still pick it from the
dropdown, which this PR makes always available.

Part of the new-vs-classic KBM editor parity work. Other draft PRs from
the same work: #51147 (language), #51148 (engine suspend) and #51149
(orphaned-key warning).

<!-- Describe how you validated the behavior. Add automated tests
wherever possible, but list manual validation steps taken as well -->
## Validation Steps Performed

- `tools\build\build.cmd` in
`src\modules\keyboardmanager\KeyboardManagerEditorUI` (arm64 Debug):
passed, exit code 0.
- No new unit tests: the changes are in UI and hook plumbing, which the
existing unit test project doesn't cover.
- Manually tested on an arm64 Debug dev runner: new mappings show the
caption hint and "Select key" for both trigger and action. The search
box gets focus when the flyout opens, and typing "k" filters the list.
Typing in the search box doesn't start key recording.
- Other manual checks:
- Add a new mapping and confirm the empty trigger and action show
"Select key". Pick a media key (e.g. Play/Pause) from the dropdown and
save.
- Start recording, switch to another app and type there. Confirm nothing
is recorded in the editor.
- Start recording and press Tab. Confirm recording stops, the toggle
unchecks and focus moves on. Record a key, release it, then press Tab:
the recorded key stays. Press Shift+Tab: focus moves backward.
- Start recording and stop without pressing a key. Confirm the previous
keys come back.

---------

Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
Copilot-Session: 4c7f42fd-7fad-43c2-9281-e581ddfd45eb
(cherry picked from commit 9501b98)
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Yu Leng (moooyo) pushed a commit that referenced this pull request Nov 3, 2026
<!-- Enter a brief description/summary of your PR here. What does it
fix/what does it change/how was it tested (even manually, if necessary)?
-->
## Summary of the Pull Request

The new Keyboard Manager editor (WinUI 3) ignored the PowerToys language
setting and always used the OS region language. It now applies
`LanguageHelper.LoadLanguage()` as
`ApplicationLanguages.PrimaryLanguageOverride` at startup, the same way
other WinUI 3 modules do (e.g. File Locksmith).

<!-- Please review the items on the PR checklist before submitting-->
## PR Checklist

- [x] Closes: #50071
<!-- - [ ] Closes: #yyy (add separate lines for additional resolved
issues) -->
- [ ] **Communication:** I've discussed this with core contributors
already. If the work hasn't been agreed, this work might be rejected
- [ ] **Tests:** Added/updated and all pass
- [x] **Localization:** All end-user-facing strings can be localized
- [ ] **Dev docs:** Added/updated
- [ ] **New binaries:** Added on the required places
- [ ] [JSON for
signing](https://github.com/microsoft/PowerToys/blob/main/.pipelines/ESRPSigning_core.json)
for new binaries
- [ ] [WXS for
installer](https://github.com/microsoft/PowerToys/blob/main/installer/PowerToysSetup/Product.wxs)
for new binaries and localization folder
- [ ] [YML for CI
pipeline](https://github.com/microsoft/PowerToys/blob/main/.pipelines/ci/templates/build-powertoys-steps.yml)
for new test projects
- [ ] [YML for signed
pipeline](https://github.com/microsoft/PowerToys/blob/main/.pipelines/release.yml)
- [ ] **Documentation updated:** If checked, please file a pull request
on [our docs
repo](https://github.com/MicrosoftDocs/windows-uwp/tree/docs/hub/powertoys)
and link it here: #xxx

<!-- Provide a more detailed description of the PR, other things fixed,
or any additional comments/features here -->
## Detailed Description of the Pull Request / Additional comments

The override is set at the top of the `App()` constructor, before any
resources load. Only `KeyboardManagerEditorXAML/App.xaml.cs` changes.

Part of the new-vs-classic KBM editor parity work. Other draft PRs from
the same work: #51148 (engine suspend), #51149 (orphaned-key warning),
and #51150 (empty key slot + recording fixes).

<!-- Describe how you validated the behavior. Add automated tests
wherever possible, but list manual validation steps taken as well -->
## Validation Steps Performed

- `tools\build\build.cmd` in
`src\modules\keyboardmanager\KeyboardManagerEditorUI` (arm64 Debug):
passed, exit code 0.
- Manually tested: set PowerToys to a language other than the OS region
language and confirm the editor UI follows it:

<img width="1370" height="446" alt="Image"
src="https://github.com/user-attachments/assets/dd68996b-d1b7-4144-ab23-39c900d6f20b"
/>

Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

3 participants