Files
PowerToys/tools/module_loader/src/HotkeyManager.h
Mike Hall 452e0dcf51 Module Loader tool for rapid testing of modules (#43813)
## Summary of the Pull Request
ModuleLoader tool, a stand-alone Win32 executable for testing of
PowerToy modules without needing branch builds.

sample output from running the tool is below:

.\ModuleLoader.exe .\powertoys.cursorwrap.dll
PowerToys Module Loader v1.0
=============================

Loading module: .\powertoys.cursorwrap.dll
Detected module name: cursorwrap

Loading settings...
Trying settings path:
C:\Users\mikehall\AppData\Local\Microsoft\PowerToys\cursorwrap\settings.json
Settings file loaded (315 characters)
Settings loaded successfully.

Loading module DLL...
Module instance created successfully
Module DLL loaded successfully.
Module key: CursorWrap
Module name: CursorWrap

Applying settings to module...
Settings applied.

Registering module hotkeys...
Module reports 1 legacy hotkey(s)
  Registering hotkey 0: Win+Alt+U - OK
Hotkeys registered: 1

Enabling module...
Module enabled.

=============================
Module is now running!
=============================

Module Status:
  - Name: CursorWrap
  - Key: CursorWrap
  - Enabled: Yes
  - Hotkeys: 1 registered

Registered Hotkeys:
  Win+Alt+U

Press Ctrl+C to exit.
You can press the module's hotkey to toggle its functionality.

Note that this doesn't integrate with Powertoys settings UI - this is
purely to test Powertoys module functionality.

## PR Checklist

- [ ] Closes: #xxx
<!-- - [ ] 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
- [ ] **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

## Detailed Description of the Pull Request / Additional comments
See details above.

## Validation Steps Performed
ModuleLoader tested on Windows 11, Surface Laptop 7 Pro.
2025-11-26 22:08:34 +08:00

87 lines
2.4 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.
#pragma once
#include <Windows.h>
#include <string>
#include <vector>
#include <map>
#include "ModuleLoader.h"
/// <summary>
/// Manages hotkey registration using RegisterHotKey API
/// </summary>
class HotkeyManager
{
public:
HotkeyManager();
~HotkeyManager();
// Prevent copying
HotkeyManager(const HotkeyManager&) = delete;
HotkeyManager& operator=(const HotkeyManager&) = delete;
/// <summary>
/// Register all hotkeys from a module
/// </summary>
/// <param name="moduleLoader">Module to get hotkeys from</param>
/// <returns>True if at least one hotkey was registered</returns>
bool RegisterModuleHotkeys(ModuleLoader& moduleLoader);
/// <summary>
/// Unregister all hotkeys
/// </summary>
void UnregisterAll();
/// <summary>
/// Handle a WM_HOTKEY message
/// </summary>
/// <param name="hotkeyId">ID from the WM_HOTKEY message</param>
/// <param name="moduleLoader">Module to trigger the hotkey on</param>
/// <returns>True if the hotkey was handled</returns>
bool HandleHotkey(int hotkeyId, ModuleLoader& moduleLoader);
/// <summary>
/// Get the number of registered hotkeys
/// </summary>
/// <returns>Number of registered hotkeys</returns>
size_t GetRegisteredCount() const { return m_registeredHotkeys.size() + (m_hotkeyExRegistered ? 1 : 0); }
/// <summary>
/// Print registered hotkeys to console
/// </summary>
void PrintHotkeys() const;
private:
struct HotkeyInfo
{
int id = 0;
size_t moduleHotkeyId = 0;
UINT modifiers = 0;
UINT vkCode = 0;
std::wstring description;
};
std::vector<HotkeyInfo> m_registeredHotkeys;
int m_nextHotkeyId;
bool m_hotkeyExRegistered;
int m_hotkeyExId;
/// <summary>
/// Convert modifier bools to RegisterHotKey modifiers
/// </summary>
UINT ConvertModifiers(bool win, bool ctrl, bool alt, bool shift) const;
/// <summary>
/// Get a string representation of modifiers
/// </summary>
std::wstring ModifiersToString(UINT modifiers) const;
/// <summary>
/// Get a string representation of a virtual key code
/// </summary>
std::wstring VKeyToString(UINT vkCode) const;
};