[Shortcut Guide] Activate with Windows key press (#13342)

* [Shortcut Guide] Activate with Windows key press

* fix spellchecker

* pr comments: fix search and add lock

* Add activation method combo box

* fix spellchecker issue for customized

* Standardize centralized hotkeys file names

* Add warning when using the long win key method

* Address PR feedback on text

* More PR feedback
This commit is contained in:
Jaime Bernardo
2021-09-23 14:23:22 +01:00
committed by GitHub
parent a0ebe5ed54
commit f647223e94
15 changed files with 308 additions and 20 deletions

View File

@@ -1,5 +1,5 @@
#include "pch.h"
#include "CentralizedHotkeys.h"
#include "centralized_hotkeys.h"
#include <map>
#include <common/logger/logger.h>

View File

@@ -3,6 +3,7 @@
#include <common/debug_control.h>
#include <common/utils/winapi_error.h>
#include <common/logger/logger.h>
#include <common/interop/shared_constants.h>
namespace CentralizedKeyboardHook
{
@@ -22,6 +23,30 @@ namespace CentralizedKeyboardHook
std::mutex mutex;
HHOOK hHook{};
// To store information about handling pressed keys.
struct PressedKeyDescriptor
{
DWORD virtualKey; // Virtual Key code of the key we're keeping track of.
std::wstring moduleName;
std::function<bool()> action;
UINT_PTR idTimer; // Timer ID for calling SET_TIMER with.
UINT millisecondsToPress; // How much time the key must be pressed.
bool operator<(const PressedKeyDescriptor& other) const
{
// We'll use the virtual key as the real key, since looking for a hit with the key is done in the more time sensitive path (low level keyboard hook).
return virtualKey < other.virtualKey;
};
};
std::multiset<PressedKeyDescriptor> pressedKeyDescriptors;
std::mutex pressedKeyMutex;
// keep track of last pressed key, to detect repeated keys and if there are more keys pressed.
const DWORD VK_DISABLED = CommonSharedConstants::VK_DISABLED;
DWORD vkCodePressed = VK_DISABLED;
// Save the runner window handle for registering timers.
HWND runnerWindow;
struct DestroyOnExit
{
~DestroyOnExit()
@@ -30,15 +55,88 @@ namespace CentralizedKeyboardHook
}
} destroyOnExitObj;
// Handle the pressed key proc
void PressedKeyTimerProc(
HWND hwnd,
UINT message,
UINT_PTR idTimer,
DWORD dwTime)
{
std::multiset<PressedKeyDescriptor> copy;
{
// Make a copy, to look for the action to call.
std::unique_lock lock{ pressedKeyMutex };
copy = pressedKeyDescriptors;
}
for (const auto& it : copy)
{
if (it.idTimer == idTimer)
{
it.action();
}
}
KillTimer(hwnd, idTimer);
}
LRESULT CALLBACK KeyboardHookProc(_In_ int nCode, _In_ WPARAM wParam, _In_ LPARAM lParam)
{
if (nCode < 0 || ((wParam != WM_KEYDOWN) && (wParam != WM_SYSKEYDOWN)))
if (nCode < 0)
{
return CallNextHookEx(hHook, nCode, wParam, lParam);
}
const auto& keyPressInfo = *reinterpret_cast<KBDLLHOOKSTRUCT*>(lParam);
// Check if the keys are pressed.
if (!pressedKeyDescriptors.empty())
{
bool wasKeyPressed = vkCodePressed != VK_DISABLED;
// Hold the lock for the shortest possible duration
if ((wParam == WM_KEYDOWN || wParam == WM_SYSKEYDOWN))
{
if (!wasKeyPressed)
{
// If no key was pressed before, let's start a timer to take into account this new key.
std::unique_lock lock{ pressedKeyMutex };
PressedKeyDescriptor dummy{ .virtualKey = keyPressInfo.vkCode };
auto [it, last] = pressedKeyDescriptors.equal_range(dummy);
for (; it != last; ++it)
{
SetTimer(runnerWindow, it->idTimer, it->millisecondsToPress, PressedKeyTimerProc);
}
}
else if (vkCodePressed != keyPressInfo.vkCode)
{
// If a different key was pressed, let's clear the timers we have started for the previous key.
std::unique_lock lock{ pressedKeyMutex };
PressedKeyDescriptor dummy{ .virtualKey = vkCodePressed };
auto [it, last] = pressedKeyDescriptors.equal_range(dummy);
for (; it != last; ++it)
{
KillTimer(runnerWindow, it->idTimer);
}
}
vkCodePressed = keyPressInfo.vkCode;
}
if (wParam == WM_KEYUP || wParam == WM_SYSKEYUP)
{
std::unique_lock lock{ pressedKeyMutex };
PressedKeyDescriptor dummy{ .virtualKey = keyPressInfo.vkCode };
auto [it, last] = pressedKeyDescriptors.equal_range(dummy);
for (; it != last; ++it)
{
KillTimer(runnerWindow, it->idTimer);
}
vkCodePressed = 0x100;
}
}
if ((wParam != WM_KEYDOWN) && (wParam != WM_SYSKEYDOWN))
{
return CallNextHookEx(hHook, nCode, wParam, lParam);
}
Hotkey hotkey{
.win = (GetAsyncKeyState(VK_LWIN) & 0x8000) || (GetAsyncKeyState(VK_RWIN) & 0x8000),
.ctrl = static_cast<bool>(GetAsyncKeyState(VK_CONTROL) & 0x8000),
@@ -85,20 +183,48 @@ namespace CentralizedKeyboardHook
hotkeyDescriptors.insert({ .hotkey = hotkey, .moduleName = moduleName, .action = std::move(action) });
}
void AddPressedKeyAction(const std::wstring& moduleName, const DWORD vk, const UINT milliseconds, std::function<bool()>&& action) noexcept
{
// Calculate a unique TimerID.
auto hash = std::hash<std::wstring>{}(moduleName); // Hash the module as the upper part of the timer ID.
const UINT upperId = hash & 0xFFFF;
const UINT lowerId = vk & 0xFFFF; // The key to press can be the lower ID.
const UINT timerId = upperId << 16 | lowerId;
std::unique_lock lock{ pressedKeyMutex };
pressedKeyDescriptors.insert({ .virtualKey = vk, .moduleName = moduleName, .action = std::move(action), .idTimer = timerId, .millisecondsToPress = milliseconds });
}
void ClearModuleHotkeys(const std::wstring& moduleName) noexcept
{
Logger::trace(L"UnRegister hotkey action for {}", moduleName);
std::unique_lock lock{ mutex };
auto it = hotkeyDescriptors.begin();
while (it != hotkeyDescriptors.end())
{
if (it->moduleName == moduleName)
std::unique_lock lock{ mutex };
auto it = hotkeyDescriptors.begin();
while (it != hotkeyDescriptors.end())
{
it = hotkeyDescriptors.erase(it);
if (it->moduleName == moduleName)
{
it = hotkeyDescriptors.erase(it);
}
else
{
++it;
}
}
else
}
{
std::unique_lock lock{ pressedKeyMutex };
auto it = pressedKeyDescriptors.begin();
while (it != pressedKeyDescriptors.end())
{
++it;
if (it->moduleName == moduleName)
{
it = pressedKeyDescriptors.erase(it);
}
else
{
++it;
}
}
}
}
@@ -131,4 +257,9 @@ namespace CentralizedKeyboardHook
hHook = NULL;
}
}
void RegisterWindow(HWND hwnd) noexcept
{
runnerWindow = hwnd;
}
}

View File

@@ -9,5 +9,7 @@ namespace CentralizedKeyboardHook
void Start() noexcept;
void Stop() noexcept;
void SetHotkeyAction(const std::wstring& moduleName, const Hotkey& hotkey, std::function<bool()>&& action) noexcept;
void AddPressedKeyAction(const std::wstring& moduleName, const DWORD vk, const UINT milliseconds, std::function<bool()>&& action) noexcept;
void ClearModuleHotkeys(const std::wstring& moduleName) noexcept;
void RegisterWindow(HWND hwnd) noexcept;
};

View File

@@ -32,7 +32,7 @@
#include <Psapi.h>
#include <RestartManager.h>
#include "centralized_kb_hook.h"
#include "CentralizedHotkeys.h"
#include "centralized_hotkeys.h"
#if _DEBUG && _WIN64
#include "unhandled_exception_handler.h"

View File

@@ -1,7 +1,7 @@
#include "pch.h"
#include "powertoy_module.h"
#include "centralized_kb_hook.h"
#include "CentralizedHotkeys.h"
#include "centralized_hotkeys.h"
#include <common/logger/logger.h>
#include <common/utils/winapi_error.h>
@@ -84,4 +84,19 @@ void PowertoyModule::UpdateHotkeyEx()
CentralizedHotkeys::AddHotkeyAction({ hotkey.modifiersMask, hotkey.vkCode }, { pt_module->get_key(), action });
}
// HACK:
// Just for enabling the shortcut guide legacy behavior of pressing the Windows Key.
// This is not the sort of behavior we'd like to have generalized on other modules.
// But this was a way to bring back the long windows key behavior that the community wanted back while maintaining the separate process.
if (pt_module->keep_track_of_pressed_win_key())
{
auto modulePtr = pt_module.get();
auto action = [modulePtr] {
modulePtr->OnHotkeyEx();
return false;
};
CentralizedKeyboardHook::AddPressedKeyAction(pt_module->get_key(), VK_LWIN, pt_module->milliseconds_win_key_must_be_pressed(), action);
CentralizedKeyboardHook::AddPressedKeyAction(pt_module->get_key(), VK_RWIN, pt_module->milliseconds_win_key_must_be_pressed(), action);
}
}

View File

@@ -47,7 +47,7 @@
<ItemGroup>
<ClCompile Include="..\common\interop\two_way_pipe_message_ipc.cpp" />
<ClCompile Include="auto_start_helper.cpp" />
<ClCompile Include="CentralizedHotkeys.cpp" />
<ClCompile Include="centralized_hotkeys.cpp" />
<ClCompile Include="general_settings.cpp" />
<ClCompile Include="pch.cpp">
<PrecompiledHeader Condition="'$(CIBuild)'!='true'">Create</PrecompiledHeader>
@@ -66,7 +66,7 @@
<ItemGroup>
<ClInclude Include="ActionRunnerUtils.h" />
<ClInclude Include="auto_start_helper.h" />
<ClInclude Include="CentralizedHotkeys.h" />
<ClInclude Include="centralized_hotkeys.h" />
<ClInclude Include="general_settings.h" />
<ClInclude Include="pch.h" />
<ClInclude Include="centralized_kb_hook.h" />

View File

@@ -39,7 +39,7 @@
<ClCompile Include="settings_telemetry.cpp">
<Filter>Utils</Filter>
</ClCompile>
<ClCompile Include="CentralizedHotkeys.cpp">
<ClCompile Include="centralized_hotkeys.cpp">
<Filter>Utils</Filter>
</ClCompile>
</ItemGroup>
@@ -84,7 +84,7 @@
<ClInclude Include="settings_telemetry.h">
<Filter>Utils</Filter>
</ClInclude>
<ClInclude Include="CentralizedHotkeys.h">
<ClInclude Include="centralized_hotkeys.h">
<Filter>Utils</Filter>
</ClInclude>
</ItemGroup>

View File

@@ -2,7 +2,8 @@
#include "Generated files/resource.h"
#include "settings_window.h"
#include "tray_icon.h"
#include "CentralizedHotkeys.h"
#include "centralized_hotkeys.h"
#include "centralized_kb_hook.h"
#include <Windows.h>
#include <common/utils/process_path.h>
@@ -249,6 +250,7 @@ void start_tray_icon()
nullptr);
WINRT_VERIFY(hwnd);
CentralizedHotkeys::RegisterWindow(hwnd);
CentralizedKeyboardHook::RegisterWindow(hwnd);
memset(&tray_icon_data, 0, sizeof(tray_icon_data));
tray_icon_data.cbSize = sizeof(tray_icon_data);
tray_icon_data.hIcon = icon;