mirror of
https://github.com/microsoft/PowerToys.git
synced 2026-02-24 04:00:02 +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 Update project references across multiple projects to utilize `$(RepoRoot)` for paths, ensuring consistency and improving maintainability. <!-- Please review the items on the PR checklist before submitting--> ## PR Checklist - [ ] Closes: N/A - [ ] **Communication:** I've discussed this with core contributors already. If the work hasn't been agreed, this work might be rejected - [x] **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 <!-- 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 This change affects the following projects: - `src/common/ManagedCsWin32/ManagedCsWin32.csproj` - `src/common/Common.Search/Common.Search.csproj` - `src/common/AllExperiments/AllExperiments.csproj` - `src/modules/peek/Peek.Common/Peek.Common.csproj` - `src/common/UITestAutomation/UITestAutomation.csproj` - `src/common/GPOWrapperProjection/GPOWrapperProjection.csproj` - `src/modules/powerrename/PowerRenameUITest/PowerRename.UITests.csproj` - `src/common/LanguageModelProvider/LanguageModelProvider.csproj` - `src/modules/Hosts/Hosts.Tests/HostsEditor.UnitTests.csproj` - `tools/StylesReportTool/StylesReportTool.vcxproj` - `src/common/interop/interop-tests/Common.Interop.UnitTests.csproj` - `tools/MonitorReportTool/MonitorReportTool.vcxproj` - `src/common/ManagedTelemetry/Telemetry/ManagedTelemetry.csproj` - `src/modules/peek/Peek.FilePreviewer/Peek.FilePreviewer.csproj` - `src/settings-ui/Settings.UI.Controls/Settings.UI.Controls.csproj` - `src/common/Themes/Themes.vcxproj` - `src/common/COMUtils/COMUtils.vcxproj` - `src/modules/cmdpal/Tests/Microsoft.CmdPal.UITests/Microsoft.CmdPal.UITests.csproj` - `src/modules/imageresizer/tests/ImageResizer.UnitTests.csproj` The changes were validated by running existing unit tests, which all passed successfully. <!-- Describe how you validated the behavior. Add automated tests wherever possible, but list manual validation steps taken as well --> ## Validation Steps Performed Executed all unit tests related to the modified projects, confirming that all tests passed without issues. ```
90 lines
3.2 KiB
C++
90 lines
3.2 KiB
C++
#include "pch.h"
|
|
#include "HotkeyManager.h"
|
|
#include "HotkeyManager.g.cpp"
|
|
|
|
namespace winrt::PowerToys::Interop::implementation
|
|
{
|
|
HotkeyManager::HotkeyManager()
|
|
{
|
|
keyboardEventCallback = KeyboardEventCallback{ this, &HotkeyManager::KeyboardEventProc };
|
|
isActiveCallback = IsActiveCallback{ this, &HotkeyManager::IsActiveProc };
|
|
filterKeyboardCallback = FilterKeyboardEvent{ this, &HotkeyManager::FilterKeyboardProc };
|
|
keyboardHook = KeyboardHook{ keyboardEventCallback, isActiveCallback, filterKeyboardCallback };
|
|
keyboardHook.Start();
|
|
}
|
|
|
|
// When all Shortcut keys are pressed, fire the HotkeyCallback event.
|
|
void HotkeyManager::KeyboardEventProc(KeyboardEvent /*ev*/)
|
|
{
|
|
// pressedKeys always stores the latest keyboard state
|
|
auto pressedKeysHandle = GetHotkeyHandle(pressedKeys);
|
|
if (hotkeys.find(pressedKeysHandle) != hotkeys.end())
|
|
{
|
|
hotkeys[pressedKeysHandle]();
|
|
|
|
// After invoking the hotkey send a dummy key to prevent Start Menu from activating
|
|
INPUT dummyEvent[1] = {};
|
|
dummyEvent[0].type = INPUT_KEYBOARD;
|
|
dummyEvent[0].ki.wVk = 0xFF;
|
|
dummyEvent[0].ki.dwFlags = KEYEVENTF_KEYUP;
|
|
SendInput(1, dummyEvent, sizeof(INPUT));
|
|
}
|
|
}
|
|
|
|
// Hotkeys are intended to be global, therefore they are always active no matter the
|
|
// context in which the keypress occurs.
|
|
bool HotkeyManager::IsActiveProc()
|
|
{
|
|
return true;
|
|
}
|
|
bool HotkeyManager::FilterKeyboardProc(KeyboardEvent ev)
|
|
{
|
|
// Updating the pressed keys here so we know if the keypress event should be propagated or not.
|
|
pressedKeys.Win = (GetAsyncKeyState(VK_LWIN) & 0x8000) || (GetAsyncKeyState(VK_RWIN) & 0x8000);
|
|
pressedKeys.Ctrl = GetAsyncKeyState(VK_CONTROL) & 0x8000;
|
|
pressedKeys.Alt = GetAsyncKeyState(VK_MENU) & 0x8000;
|
|
pressedKeys.Shift = GetAsyncKeyState(VK_SHIFT) & 0x8000;
|
|
pressedKeys.Key = static_cast<unsigned char>(ev.key);
|
|
|
|
// Convert to hotkey handle
|
|
auto pressedKeysHandle = GetHotkeyHandle(pressedKeys);
|
|
|
|
// Check if any hotkey matches the pressed keys if the current key event is a key down event
|
|
if ((ev.message == WM_KEYDOWN || ev.message == WM_SYSKEYDOWN) && hotkeys.find(pressedKeysHandle)!=hotkeys.end())
|
|
{
|
|
return true;
|
|
}
|
|
|
|
return false;
|
|
}
|
|
|
|
uint16_t HotkeyManager::RegisterHotkey(winrt::PowerToys::Interop::Hotkey const& _hotkey, winrt::PowerToys::Interop::HotkeyCallback const& _callback)
|
|
{
|
|
auto handle = GetHotkeyHandle(_hotkey);
|
|
hotkeys[handle] = _callback;
|
|
return handle;
|
|
}
|
|
|
|
void HotkeyManager::UnregisterHotkey(uint16_t _handle)
|
|
{
|
|
auto iter = hotkeys.find(_handle);
|
|
if (iter != hotkeys.end()) {
|
|
hotkeys.erase(iter);
|
|
}
|
|
}
|
|
|
|
void HotkeyManager::Close()
|
|
{
|
|
}
|
|
|
|
uint16_t HotkeyManager::GetHotkeyHandle(Hotkey hotkey)
|
|
{
|
|
uint16_t handle = hotkey.Key;
|
|
handle |= hotkey.Win << 8;
|
|
handle |= hotkey.Ctrl << 9;
|
|
handle |= hotkey.Shift << 10;
|
|
handle |= hotkey.Alt << 11;
|
|
return handle;
|
|
}
|
|
}
|