Muyuan Li 18edd52239 Fix Peek Ctrl+W shortcut not working after clicking preview (#48293)
## Summary

Fixes #48274

When previewing a file with Peek, clicking inside the preview content
(PDF, text/code, markdown, HTML) makes **Ctrl+W unable to close the
window**. This happens because the preview controls (WebView2 for most
file types, native shell handlers for others) capture keyboard focus in
their own message loop, bypassing the XAML keyboard accelerator system
entirely.

## Problem

Peek defines keyboard shortcuts (Ctrl+W to close, Escape to close, arrow
keys to navigate) as `KeyboardAccelerator` elements on the main XAML
Grid. These only fire when keyboard input flows through the XAML input
system. However:

- **WebView2** (used for PDF, text/code via Monaco, markdown, HTML):
Runs Chromium in a separate process that consumes all keyboard input
when focused. Ctrl+W is particularly problematic because Chromium treats
it as "close tab."
- **Shell Preview Handlers** (native HWND): Run in a child Win32 window
that handles keyboard messages independently.

Once either of these controls gets focus via a mouse click, keyboard
shortcuts stop working.

## Solution

Added a **low-level keyboard hook** (`WH_KEYBOARD_LL`) that intercepts
key events at the OS level, regardless of which control has focus:

- **Ctrl+W** and **Escape** → close the Peek window
- **Arrow keys** (Left/Right/Up/Down without Ctrl) → navigate between
files

The hook is installed only while the Peek window is visible and only
acts when Peek is the foreground window, so it has no impact on other
applications.

As additional defense-in-depth, `AreBrowserAcceleratorKeysEnabled` is
set to `false` on the WebView2 control, preventing Chromium from
consuming browser-specific shortcuts like Ctrl+W.

## Changes

| File | Change |
|------|--------|
| `Peek.UI/PeekXAML/MainWindow.xaml.cs` | Install/uninstall keyboard
hook in Initialize/Uninitialize; hook callback handles Ctrl+W, Escape,
and arrow keys |
| `Peek.UI/Native/NativeMethods.cs` | P/Invoke declarations for
`SetWindowsHookEx`, `UnhookWindowsHookEx`, `CallNextHookEx`,
`GetAsyncKeyState` |
| `Peek.FilePreviewer/Controls/BrowserControl.xaml.cs` | Disable browser
accelerator keys on WebView2 |

## Validation

- [x] Ctrl+W closes Peek after clicking inside PDF preview
- [x] Ctrl+W closes Peek after clicking inside text/code preview
(Monaco)
- [x] Escape closes Peek after clicking inside preview
- [x] Arrow keys navigate between files after clicking inside preview
- [x] Normal typing/interaction inside previews still works (Ctrl+C,
scrolling, etc.)
- [x] Shortcuts still work without clicking (existing behavior
preserved)

---------

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com>
2026-07-09 17:14:26 +08:00

Microsoft PowerToys

Microsoft PowerToys is a collection of utilities that help you customize Windows and streamline everyday tasks.

Installation · Documentation · Blog · Release notes

🔨 Utilities

PowerToys includes over 30 utilities to help you customize and optimize your Windows experience:

Advanced Paste icon Advanced Paste Always on Top icon Always on Top Awake icon Awake
Color Picker icon Color Picker Command Not Found icon Command Not Found Command Palette icon Command Palette
Crop and Lock icon Crop And Lock Environment Variables icon Environment Variables FancyZones icon FancyZones
File Explorer Add-ons icon File Explorer Add-ons File Locksmith icon File Locksmith Grab And Move icon Grab And Move
Hosts File Editor icon Hosts File Editor Image Resizer icon Image Resizer Keyboard Manager icon Keyboard Manager
Light Switch icon Light Switch Mouse Utilities icon Mouse Utilities Mouse Without Borders icon Mouse Without Borders
New+ icon New+ Peek icon Peek PowerDisplay icon PowerDisplay
PowerRename icon PowerRename PowerToys Run icon PowerToys Run Quick Accent icon Quick Accent
Registry Preview icon Registry Preview Screen Ruler icon Screen Ruler Shortcut Guide icon Shortcut Guide
Text Extractor icon Text Extractor Workspaces icon Workspaces ZoomIt icon ZoomIt

📦 Installation

For detailed installation instructions and system requirements, visit the installation docs.

But to get started quickly, choose one of the installation methods below:

Download the .exe file from GitHub

Go to the PowerToys GitHub releases, scroll down and select Assets to reveal the installation files, and choose the one that matches your architecture and install scope. For most devices, that would be x64 per-user.

Microsoft Store
You can easily install PowerToys from the Microsoft Store:

WinGet
Download PowerToys from [WinGet](https://github.com/microsoft/winget-cli#installing-the-client). Updating PowerToys via winget will respect the current PowerToys installation scope. To install PowerToys, run the following command from the command line / PowerShell:
  • User scope installer (default)
winget install Microsoft.PowerToys -s winget
  • Machine-wide scope installer
winget install --scope machine Microsoft.PowerToys -s winget
Other methods
There are [community driven install methods](https://learn.microsoft.com/windows/powertoys/install#community-driven-install-tools) such as Chocolatey and Scoop. If these are your preferred install solutions, you can find the install instructions there.

What's new?

What's new image

To see what's new, check out the release notes.

🛣️ Roadmap

We are planning some nice new features and improvements for the next releases a brand-new Shortcut Guide experience, ensuring it's easier to find and install Command Palette extensions and so much more! Stay tuned for v0.100!

❤️ PowerToys Community

The PowerToys team is extremely grateful to have the support of an amazing active community. The work you do is incredibly important. PowerToys wouldn't be nearly what it is today without your help filing bugs, updating documentation, guiding the design, or writing features. We want to say thank you and take time to recognize your work. Your contributions and feedback improve PowerToys month after month!

Contributing

This project welcomes contributions of all types. Besides coding features / bug fixes, other ways to assist include spec writing, design, documentation, and finding bugs. We are excited to work with the power user community to build a set of tools for helping you get the most out of Windows. We ask that before you start work on a feature that you would like to contribute, please read our Contributor's Guide. We would be happy to work with you to figure out the best approach, provide guidance and mentorship throughout feature development, and help avoid any wasted or duplicate effort. Most contributions require you to agree to a Contributor License Agreement (CLA) declaring that you grant us the rights to use your contribution and that you have permission to do so. For guidance on developing for PowerToys, please read the developer docs for a detailed breakdown. This includes how to setup your computer to compile.

Code of conduct

This project has adopted the Microsoft Open Source Code of Conduct.

Privacy statement

The application logs basic diagnostic data (telemetry). For more privacy information and what we collect, see our PowerToys Data and Privacy documentation.

Description
Microsoft PowerToys is a collection of utilities that help you customize Windows and streamline everyday tasks
Readme MIT 748 MiB
Languages
C 48.3%
C# 23.9%
C++ 14%
JavaScript 13%
PowerShell 0.5%
Other 0.2%