mirror of
https://github.com/microsoft/PowerToys.git
synced 2025-12-15 19:27:56 +01:00
<!-- 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 pull request introduces the new KeystrokeOverlay module to PowerToys, including its documentation, build configuration, installer setup, and spell-check dictionary updates. KeystrokeOverlay can be used to show keys pressed on the screen like Visual Studio Code's Screencast Mode. It provides customization options for text size and background, offers four types of display modes, and three shortcuts for toggling on/off the display, cycling through monitors, and through different display modes (see below for more details). <!-- Please review the items on the PR checklist before submitting--> ## PR Checklist - [x] Closes: #981 <!-- - [ ] Closes: #yyy (add separate lines for additional resolved issues) --> - [x] **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 - [ ] **Localization:** All end-user-facing strings can be localized - [x] **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 **KeystrokeOverlay Module Integration:** * Added KeystrokeOverlay as a new module in the solution, with corresponding projects for the keyboard service, UI, and module interface (`PowerToys.slnx`). * Added documentation for the Keystroke Overlay module, including architecture, usage, and debugging instructions (`doc/devdocs/modules/keystrokeoverlay.md`). * Updated installer configuration to include Keystroke Overlay, adding new WiX include and setup files (`installer/PowerToysSetup/Common.wxi`, `installer/PowerToysSetup/KeystrokeOverlay.wxs`). [[1]](diffhunk://#diff-ecd2ee19d18433ed47b8f13b44cfff7b00c8009c17bc71139cec0d8571f8f607R1-R59) [[2]](diffhunk://#diff-af48b984b168acbe5aeb021e97a9e826ab9b52c20c08b36f40dd3e5ad00342b1R1-R9) * Registered Keystroke Overlay in the settings documentation and issue templates (`doc/dsc/Settings.md`, `.github/ISSUE_TEMPLATE/bug_report.yml`, `.github/ISSUE_TEMPLATE/translation_issue.yml`). [[1]](diffhunk://#diff-bde46f469b76ba994ea938853f169553846221b329a21e9b59d37c5a0b7d63aeR35) [[2]](diffhunk://#diff-637f7b97bba458badb691a1557c3d4648686292e948dbe3e8360564378b653efR85) [[3]](diffhunk://#diff-135b470e9875068a1085599402d6f89bea163068568c426b22f493f35fbfbea6R58) * Added KeystrokeOverlay binaries to the signing pipeline (`.pipelines/ESRPSigning_core.json`). **Customization options** | Category | Setting | Description | Default / Example | | :--- | :--- | :--- | :--- | | **General** | **Enable Keystroke Overlay** | Master toggle to turn the module on or off. | `On` | | **Shortcuts** | **Activation Shortcut** | Hotkey to toggle the overlay visibility. | `Win` + `Shift` + `K` | | | **Switch Monitor Shortcut** | Hotkey to move the overlay to another display. | `Win` + `Shift` + `/` | | | **Switch Display Mode** | Hotkey to cycle through different visualization modes. | `Win` + `Shift` + `D` | | **Behavior** | **Draggable Overlay** | Allows you to manually move the overlay position with your mouse. | `On` | | | **Display Mode** | Controls what history of keys is shown (e.g., history vs. single). | `Last Five Keystrokes` | | | **Overlay Timeout** | How long (in ms) the keys stay visible before fading. | `2000` ms | | **Appearance** | **Text Size** | Adjusts the font size of the keys inside the overlay. | `37` | | | **Text Color** | Sets the font color and transparency level. | `White` (Example) | | | **Background Color** | Sets the background box color and transparency level. | `Purple` (Example) | <!-- Describe how you validated the behavior. Add automated tests wherever possible, but list manual validation steps taken as well --> ## Validation Steps Performed The new module was tested on multiple different Windows Devices and all configurations work as expected. --------- Signed-off-by: Shawn Yuan (from Dev Box) <shuaiyuan@microsoft.com> Co-authored-by: Sátvik Karanam <89281036+skara9@users.noreply.github.com> Co-authored-by: Jiří Polášek <me@jiripolasek.com> Co-authored-by: Software2 <software-2@users.noreply.github.com> Co-authored-by: Niels Laute <niels.laute@live.nl> Co-authored-by: Mike Griese <migrie@microsoft.com> Co-authored-by: Jaylyn Barbee <51131738+Jaylyn-Barbee@users.noreply.github.com> Co-authored-by: Gordon Lam (SH) <yeelam@microsoft.com> Co-authored-by: Kai Tao <69313318+vanzue@users.noreply.github.com> Co-authored-by: Mark Russinovich <markruss@microsoft.com> Co-authored-by: Mark Russinovich <markruss@ntdev.microsoft.com> Co-authored-by: Clint Rutkas <clint@rutkas.com> Co-authored-by: Alex Mihaiuc <69110671+foxmsft@users.noreply.github.com> Co-authored-by: Michael Jolley <mike@baldbeardedbuilder.com> Co-authored-by: Gordon Lam <73506701+yeelam-gordon@users.noreply.github.com> Co-authored-by: leileizhang <leilzh@microsoft.com> Co-authored-by: Dave Rayment <dave.rayment@gmail.com> Co-authored-by: Shawn Yuan <128874481+shuaiyuanxx@users.noreply.github.com> Co-authored-by: Kai Tao <kaitao@microsoft.com> Co-authored-by: Copilot <198982749+Copilot@users.noreply.github.com> Co-authored-by: jiripolasek <4773077+jiripolasek@users.noreply.github.com> Co-authored-by: Dustin L. Howett <duhowett@microsoft.com> Co-authored-by: Sam Rueby <samrueby@gmail.com> Co-authored-by: Lee Won Jun <dldnjs1013@nate.com> Co-authored-by: Mason Bergstrom <13530957+MasonBergstrom@users.noreply.github.com> Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> Co-authored-by: Mike Hall <mikehall@microsoft.com> Co-authored-by: moooyo <42196638+moooyo@users.noreply.github.com> Co-authored-by: Yu Leng <yuleng@microsoft.com> Co-authored-by: dsm20 <74568547+dsm20@users.noreply.github.com> Co-authored-by: zackpaton <91792781+zackpaton@users.noreply.github.com> Co-authored-by: Gleb Khmyznikov <gleb.khmyznikov@gmail.com> Co-authored-by: Guilherme <57814418+DevLGuilherme@users.noreply.github.com>
466 lines
14 KiB
C#
466 lines
14 KiB
C#
// Copyright (c) Microsoft Corporation
|
|
// The Microsoft Corporation licenses this file to you under the MIT license.
|
|
// See the LICENSE file in the project root for more information.
|
|
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using System.Reflection;
|
|
using System.Runtime.InteropServices;
|
|
using System.Runtime.InteropServices.WindowsRuntime;
|
|
using System.Threading;
|
|
using System.Threading.Tasks;
|
|
|
|
using KeystrokeOverlayUI.Controls;
|
|
using KeystrokeOverlayUI.Helpers;
|
|
using KeystrokeOverlayUI.Models;
|
|
using KeystrokeOverlayUI.Services;
|
|
|
|
using Microsoft.UI;
|
|
using Microsoft.UI.Composition.SystemBackdrops;
|
|
using Microsoft.UI.Input;
|
|
using Microsoft.UI.Windowing;
|
|
using Microsoft.UI.Xaml;
|
|
using Microsoft.UI.Xaml.Controls;
|
|
using Microsoft.UI.Xaml.Controls.Primitives;
|
|
using Microsoft.UI.Xaml.Data;
|
|
using Microsoft.UI.Xaml.Input;
|
|
using Microsoft.UI.Xaml.Media;
|
|
using Microsoft.UI.Xaml.Navigation;
|
|
using Windows.Foundation;
|
|
using Windows.Foundation.Collections;
|
|
using WinRT.Interop;
|
|
|
|
namespace KeystrokeOverlayUI
|
|
{
|
|
// Main overlay window.
|
|
public sealed partial class MainWindow : Window, IDisposable
|
|
{
|
|
public MainViewModel ViewModel { get; set; } = new();
|
|
|
|
// readonly constants
|
|
private readonly InputCursor _dragCursor = InputSystemCursor.Create(InputSystemCursorShape.SizeAll);
|
|
private readonly DispatcherTimer _zOrderEnforcer = new();
|
|
|
|
// core components
|
|
private readonly KeystrokeListener _keystrokeListener = new();
|
|
private readonly OverlaySettings _overlaySettings = new();
|
|
private CancellationTokenSource _startupCancellationSource;
|
|
private bool _disposed;
|
|
|
|
// draggable overlay
|
|
private bool _isDragging;
|
|
private NativeWindowHelper.POINT _lastCursorPos;
|
|
|
|
public MainWindow()
|
|
{
|
|
InitializeComponent();
|
|
SystemBackdrop = new MicaBackdrop() { Kind = MicaKind.BaseAlt };
|
|
ForceWindowOnTop();
|
|
|
|
Activated += (s, e) => ApplyOverlayStyles();
|
|
_zOrderEnforcer.Interval = TimeSpan.FromMilliseconds(500);
|
|
_zOrderEnforcer.Tick += (s, e) => ForceWindowOnTop();
|
|
|
|
RootGrid.DataContext = ViewModel;
|
|
|
|
_overlaySettings.SettingsUpdated += (props) =>
|
|
{
|
|
DispatcherQueue.TryEnqueue(() => ViewModel.ApplySettings(props));
|
|
};
|
|
|
|
_overlaySettings.Initialize();
|
|
|
|
_keystrokeListener.OnBatchReceived += OnKeyReceived;
|
|
_keystrokeListener.Start();
|
|
|
|
ConfigureOverlayWindow();
|
|
RunStartupSequence(isDraggable: ViewModel.IsDraggable);
|
|
|
|
ViewModel.HotkeyActionTriggered += OnHotkeyActionTriggered;
|
|
|
|
ViewModel.PropertyChanged += (s, e) =>
|
|
{
|
|
if (e.PropertyName == nameof(MainViewModel.IsDraggable))
|
|
{
|
|
RunStartupSequence(isDraggable: ViewModel.IsDraggable);
|
|
}
|
|
};
|
|
}
|
|
|
|
private void ApplyOverlayStyles()
|
|
{
|
|
IntPtr hWnd = WindowNative.GetWindowHandle(this);
|
|
NativeWindowHelper.SetOverlayWindowStyles(hWnd);
|
|
NativeWindowHelper.SetRoundedCorners(hWnd);
|
|
|
|
ForceWindowOnTop();
|
|
}
|
|
|
|
private void ConfigureOverlayWindow()
|
|
{
|
|
var appWindow = GetAppWindow();
|
|
|
|
if (appWindow != null)
|
|
{
|
|
var presenter = appWindow.Presenter as OverlappedPresenter
|
|
?? OverlappedPresenter.Create();
|
|
|
|
appWindow.SetPresenter(presenter);
|
|
|
|
presenter.IsAlwaysOnTop = true;
|
|
presenter.IsResizable = false;
|
|
presenter.IsMinimizable = false;
|
|
presenter.IsMaximizable = false;
|
|
presenter.SetBorderAndTitleBar(false, false);
|
|
}
|
|
}
|
|
|
|
private async void RunStartupSequence(bool isDraggable)
|
|
{
|
|
_startupCancellationSource?.Cancel();
|
|
_startupCancellationSource?.Dispose();
|
|
_startupCancellationSource = new CancellationTokenSource();
|
|
|
|
var token = _startupCancellationSource.Token;
|
|
|
|
ForceWindowOnTop();
|
|
if (!_zOrderEnforcer.IsEnabled)
|
|
{
|
|
_zOrderEnforcer.Start();
|
|
}
|
|
|
|
try
|
|
{
|
|
if (isDraggable)
|
|
{
|
|
// Loop with cancellation check
|
|
for (int index = 10; index > 0; index--)
|
|
{
|
|
ViewModel.RegisterKey($"Drag to Position ({index})", durationMs: 1000, textSize: 30);
|
|
await Task.Delay(1000, token);
|
|
}
|
|
}
|
|
|
|
// cleanup
|
|
ViewModel.ClearKeys();
|
|
await Task.Delay(500, token);
|
|
}
|
|
catch (OperationCanceledException)
|
|
{
|
|
ViewModel.ClearKeys();
|
|
}
|
|
finally
|
|
{
|
|
// cleanup
|
|
if (_startupCancellationSource != null)
|
|
{
|
|
_startupCancellationSource.Dispose();
|
|
_startupCancellationSource = null;
|
|
}
|
|
}
|
|
}
|
|
|
|
private void OnKeyReceived(KeystrokeEvent kEvent)
|
|
{
|
|
if (_startupCancellationSource != null && !_startupCancellationSource.IsCancellationRequested)
|
|
{
|
|
_startupCancellationSource.Cancel();
|
|
}
|
|
|
|
DispatcherQueue.TryEnqueue(() =>
|
|
{
|
|
ViewModel.HandleKeystrokeEvent(kEvent);
|
|
|
|
if (!_zOrderEnforcer.IsEnabled)
|
|
{
|
|
_zOrderEnforcer.Start();
|
|
}
|
|
});
|
|
}
|
|
|
|
// ----------------------
|
|
// Hotkey Methods
|
|
// ----------------------
|
|
private void OnHotkeyActionTriggered(object sender, HotkeyAction action)
|
|
{
|
|
switch (action)
|
|
{
|
|
case HotkeyAction.Monitor:
|
|
MoveToNextMonitor();
|
|
break;
|
|
case HotkeyAction.DisplayMode:
|
|
case HotkeyAction.Activation:
|
|
HandleActivation();
|
|
break;
|
|
}
|
|
|
|
ForceWindowOnTop();
|
|
|
|
// resize to show labels
|
|
RootGrid.Measure(new Size(double.PositiveInfinity, double.PositiveInfinity));
|
|
var desiredSize = RootGrid.DesiredSize;
|
|
ResizeAppWindow(desiredSize.Width + 5, desiredSize.Height + 15);
|
|
}
|
|
|
|
private void HandleActivation()
|
|
{
|
|
ShowAppWindow();
|
|
}
|
|
|
|
private void MoveToNextMonitor()
|
|
{
|
|
IntPtr hWnd = WindowNative.GetWindowHandle(this);
|
|
WindowId wndId = Win32Interop.GetWindowIdFromWindow(hWnd);
|
|
AppWindow appWindow = AppWindow.GetFromWindowId(wndId);
|
|
|
|
if (appWindow == null)
|
|
{
|
|
return;
|
|
}
|
|
|
|
var displayAreas = DisplayArea.FindAll();
|
|
if (displayAreas.Count <= 1)
|
|
{
|
|
return;
|
|
}
|
|
|
|
// Find current display index
|
|
DisplayArea currentDisplay = DisplayArea.GetFromWindowId(wndId, DisplayAreaFallback.Primary);
|
|
|
|
int currentIndex = -1;
|
|
for (int i = 0; i < displayAreas.Count; i++)
|
|
{
|
|
if (displayAreas[i].DisplayId.Value == currentDisplay.DisplayId.Value)
|
|
{
|
|
currentIndex = i;
|
|
break;
|
|
}
|
|
}
|
|
|
|
// Calculate Next Index
|
|
int nextIndex = (currentIndex + 1) % displayAreas.Count;
|
|
DisplayArea nextDisplay = displayAreas[nextIndex];
|
|
|
|
// move to Top-Left of new monitor
|
|
int newX = nextDisplay.WorkArea.X + 15;
|
|
int newY = nextDisplay.WorkArea.Y + 12;
|
|
|
|
appWindow.Move(new Windows.Graphics.PointInt32(newX, newY));
|
|
ViewModel.ShowLabel(HotkeyAction.Monitor, $"Monitor {nextIndex + 1}");
|
|
}
|
|
|
|
// ----------------------
|
|
// Draggable Overlay
|
|
// ----------------------
|
|
private void SetRootGridCursor(InputCursor cursor)
|
|
{
|
|
// Use Reflection to access the protected "ProtectedCursor" property on the Border (RootGrid)
|
|
typeof(UIElement)
|
|
.GetProperty("ProtectedCursor", BindingFlags.NonPublic | BindingFlags.Instance)
|
|
?.SetValue(RootGrid, cursor);
|
|
}
|
|
|
|
private void RootGrid_PointerPressed(object sender, PointerRoutedEventArgs e)
|
|
{
|
|
var properties = e.GetCurrentPoint(RootGrid).Properties;
|
|
|
|
if (ViewModel.IsDraggable && properties.IsLeftButtonPressed)
|
|
{
|
|
_isDragging = true;
|
|
RootGrid.CapturePointer(e.Pointer);
|
|
_lastCursorPos = NativeWindowHelper.GetCursorPosition();
|
|
|
|
SetRootGridCursor(_dragCursor);
|
|
e.Handled = true;
|
|
}
|
|
}
|
|
|
|
private void RootGrid_PointerMoved(object sender, PointerRoutedEventArgs e)
|
|
{
|
|
if (_isDragging)
|
|
{
|
|
var currentPos = NativeWindowHelper.GetCursorPosition();
|
|
|
|
int deltaX = currentPos.X - _lastCursorPos.X;
|
|
int deltaY = currentPos.Y - _lastCursorPos.Y;
|
|
|
|
var appWindow = GetAppWindow();
|
|
if (appWindow != null)
|
|
{
|
|
var newPos = new Windows.Graphics.PointInt32(
|
|
appWindow.Position.X + deltaX,
|
|
appWindow.Position.Y + deltaY );
|
|
|
|
appWindow.Move(newPos);
|
|
}
|
|
|
|
_lastCursorPos = currentPos;
|
|
}
|
|
}
|
|
|
|
private void RootGrid_PointerReleased(object sender, PointerRoutedEventArgs e)
|
|
{
|
|
if (_isDragging)
|
|
{
|
|
_isDragging = false;
|
|
RootGrid.ReleasePointerCapture(e.Pointer);
|
|
UpdateCursorState();
|
|
}
|
|
}
|
|
|
|
private void RootGrid_PointerEntered(object sender, PointerRoutedEventArgs e)
|
|
{
|
|
UpdateCursorState();
|
|
}
|
|
|
|
private void RootGrid_PointerExited(object sender, PointerRoutedEventArgs e)
|
|
{
|
|
SetRootGridCursor(null);
|
|
}
|
|
|
|
private void UpdateCursorState()
|
|
{
|
|
if (ViewModel.IsDraggable)
|
|
{
|
|
SetRootGridCursor(_dragCursor);
|
|
}
|
|
else
|
|
{
|
|
SetRootGridCursor(null);
|
|
}
|
|
}
|
|
|
|
// ----------------------
|
|
// WinUI Event Handlers
|
|
// ----------------------
|
|
private void RootGrid_Loaded(object sender, RoutedEventArgs e)
|
|
{
|
|
RootGrid.Measure(new Size(double.PositiveInfinity, double.PositiveInfinity));
|
|
var desiredSize = RootGrid.DesiredSize;
|
|
ResizeAppWindow(desiredSize.Width + 5, desiredSize.Height + 15);
|
|
}
|
|
|
|
private void StackPanel_SizeChanged(object sender, SizeChangedEventArgs e)
|
|
{
|
|
if (sender is not StackPanel stackPanel)
|
|
{
|
|
return;
|
|
}
|
|
|
|
stackPanel.Measure(new Size(double.PositiveInfinity, double.PositiveInfinity));
|
|
var desiredSize = stackPanel.DesiredSize;
|
|
|
|
if (desiredSize.Width == 0 ||
|
|
desiredSize.Height == 0 ||
|
|
ViewModel.PressedKeys.Count == 0)
|
|
{
|
|
if (!ViewModel.IsVisibleHotkey)
|
|
{
|
|
HideAppWindow();
|
|
_zOrderEnforcer.Stop();
|
|
}
|
|
|
|
return;
|
|
}
|
|
|
|
ShowAppWindow();
|
|
|
|
double totalWidth =
|
|
desiredSize.Width + RootGrid.Padding.Left + RootGrid.Padding.Right + 5;
|
|
|
|
double totalHeight =
|
|
desiredSize.Height + RootGrid.Padding.Top + RootGrid.Padding.Bottom + 15;
|
|
|
|
if (ViewModel.IsVisibleHotkey)
|
|
{
|
|
totalHeight = totalHeight + 30;
|
|
}
|
|
|
|
ResizeAppWindow(totalWidth, totalHeight);
|
|
ForceWindowOnTop();
|
|
}
|
|
|
|
// ----------------------
|
|
// Window Helper Methods
|
|
// ----------------------
|
|
private AppWindow GetAppWindow()
|
|
{
|
|
IntPtr hWnd = WindowNative.GetWindowHandle(this);
|
|
WindowId wndId = Win32Interop.GetWindowIdFromWindow(hWnd);
|
|
return AppWindow.GetFromWindowId(wndId);
|
|
}
|
|
|
|
private void HideAppWindow()
|
|
{
|
|
var appWindow = GetAppWindow();
|
|
appWindow?.Hide();
|
|
}
|
|
|
|
private void ShowAppWindow()
|
|
{
|
|
var appWindow = GetAppWindow();
|
|
appWindow?.Show();
|
|
}
|
|
|
|
private void ResizeAppWindow(double widthDIPs, double heightDIPs)
|
|
{
|
|
var appWindow = GetAppWindow();
|
|
|
|
if (appWindow != null)
|
|
{
|
|
double scale = RootGrid.XamlRoot.RasterizationScale;
|
|
|
|
int windowWidth = (int)Math.Ceiling(widthDIPs * scale);
|
|
int windowHeight = (int)Math.Ceiling(heightDIPs * scale);
|
|
|
|
if (appWindow.Size.Width != windowWidth ||
|
|
appWindow.Size.Height != windowHeight)
|
|
{
|
|
appWindow.Resize(new Windows.Graphics.SizeInt32(windowWidth, windowHeight));
|
|
}
|
|
}
|
|
}
|
|
|
|
private void ForceWindowOnTop()
|
|
{
|
|
if (ViewModel.PressedKeys.Count == 0)
|
|
{
|
|
_zOrderEnforcer.Stop();
|
|
return;
|
|
}
|
|
|
|
IntPtr hWnd = WindowNative.GetWindowHandle(this);
|
|
NativeWindowHelper.EnforceTopMost(hWnd);
|
|
}
|
|
|
|
// -------------------
|
|
// Other Methods
|
|
// -------------------
|
|
public void Dispose()
|
|
{
|
|
Dispose(true);
|
|
GC.SuppressFinalize(this);
|
|
}
|
|
|
|
private void Dispose(bool disposing)
|
|
{
|
|
if (!_disposed)
|
|
{
|
|
if (disposing)
|
|
{
|
|
_keystrokeListener?.Dispose();
|
|
_overlaySettings?.Dispose();
|
|
}
|
|
|
|
_disposed = true;
|
|
}
|
|
}
|
|
|
|
~MainWindow()
|
|
{
|
|
Dispose(false);
|
|
}
|
|
}
|
|
}
|