[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
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
15 changed files with 308 additions and 20 deletions

View file

@ -64,7 +64,7 @@ public:
PowerToysSettings::PowerToyValues values =
PowerToysSettings::PowerToyValues::from_json_string(config, get_key());
ParseHotkey(values);
ParseSettings(values);
}
catch (std::exception ex)
{
@ -119,6 +119,10 @@ public:
virtual std::optional<HotkeyEx> GetHotkeyEx() override
{
Logger::trace("GetHotkeyEx()");
if (m_shouldReactToPressedWinKey)
{
return std::nullopt;
}
return m_hotkey;
}
@ -154,6 +158,16 @@ public:
}
}
virtual bool keep_track_of_pressed_win_key() override
{
return m_shouldReactToPressedWinKey;
}
virtual UINT milliseconds_win_key_must_be_pressed() override
{
return m_millisecondsWinKeyShouldBePressed;
}
private:
std::wstring app_name;
//contains the non localized key of the powertoy
@ -163,6 +177,12 @@ private:
// Hotkey to invoke the module
HotkeyEx m_hotkey;
// If the module should be activated through the legacy pressing windows key behavior.
const UINT DEFAULT_MILLISECONDS_WIN_KEY_SHOULD_BE_PRESSED = 900;
bool m_shouldReactToPressedWinKey = false;
UINT m_millisecondsWinKeyShouldBePressed = DEFAULT_MILLISECONDS_WIN_KEY_SHOULD_BE_PRESSED;
HANDLE exitEvent;
bool StartProcess(std::wstring args = L"")
@ -239,7 +259,7 @@ private:
PowerToysSettings::PowerToyValues settings =
PowerToysSettings::PowerToyValues::load_from_settings_file(app_key);
ParseHotkey(settings);
ParseSettings(settings);
}
catch (std::exception ex)
{
@ -251,13 +271,17 @@ private:
}
}
void ParseHotkey(PowerToysSettings::PowerToyValues& settings)
void ParseSettings(PowerToysSettings::PowerToyValues& settings)
{
m_shouldReactToPressedWinKey = false;
m_millisecondsWinKeyShouldBePressed = DEFAULT_MILLISECONDS_WIN_KEY_SHOULD_BE_PRESSED;
auto settingsObject = settings.get_raw_json();
if (settingsObject.GetView().Size())
{
try
{
// Parse HotKey
auto jsonHotkeyObject = settingsObject.GetNamedObject(L"properties").GetNamedObject(L"open_shortcutguide");
auto hotkey = PowerToysSettings::HotkeyObject::from_json(jsonHotkeyObject);
m_hotkey = HotkeyEx();
@ -287,6 +311,18 @@ private:
{
Logger::warn("Failed to initialize Shortcut Guide start shortcut");
}
try
{
// Parse Legacy windows key press behavior settings
auto jsonUseLegacyWinKeyBehaviorObject = settingsObject.GetNamedObject(L"properties").GetNamedObject(L"use_legacy_press_win_key_behavior");
m_shouldReactToPressedWinKey = (bool)jsonUseLegacyWinKeyBehaviorObject.GetNamedBoolean(L"value");
auto jsonPressTimeObject = settingsObject.GetNamedObject(L"properties").GetNamedObject(L"press_time");
m_millisecondsWinKeyShouldBePressed = (UINT)jsonPressTimeObject.GetNamedNumber(L"value");
}
catch (...)
{
Logger::warn("Failed to get legacy win key behavior settings");
}
}
else
{

View file

@ -98,6 +98,14 @@ public:
*/
virtual bool on_hotkey(size_t hotkeyId) { return false; }
/* These are for enabling the legacy behavior of showing the shortcut guide after pressing the win key.
* keep_track_of_pressed_win_key returns true if the module wants to keep track of the win key being pressed.
* milliseconds_win_key_must_be_pressed returns the number of milliseconds the win key should be pressed before triggering the module.
* Don't use these for new modules.
*/
virtual bool keep_track_of_pressed_win_key() { return false; }
virtual UINT milliseconds_win_key_must_be_pressed() { return 0; }
virtual void send_settings_telemetry()
{
}

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;

View file

@ -11,6 +11,7 @@ namespace Microsoft.PowerToys.Settings.UI.Library
public ShortcutGuideProperties()
{
OverlayOpacity = new IntProperty(90);
UseLegacyPressWinKeyBehavior = new BoolProperty(false);
PressTime = new IntProperty(900);
Theme = new StringProperty("system");
DisabledApps = new StringProperty();
@ -23,6 +24,9 @@ namespace Microsoft.PowerToys.Settings.UI.Library
[JsonPropertyName("overlay_opacity")]
public IntProperty OverlayOpacity { get; set; }
[JsonPropertyName("use_legacy_press_win_key_behavior")]
public BoolProperty UseLegacyPressWinKeyBehavior { get; set; }
[JsonPropertyName("press_time")]
public IntProperty PressTime { get; set; }

View file

@ -52,6 +52,7 @@ namespace Microsoft.PowerToys.Settings.UI.Library.ViewModels
SendConfigMSG = ipcMSGCallBackFunc;
_isEnabled = GeneralSettingsConfig.Enabled.ShortcutGuide;
_useLegacyPressWinKeyBehavior = Settings.Properties.UseLegacyPressWinKeyBehavior.Value;
_pressTime = Settings.Properties.PressTime.Value;
_opacity = Settings.Properties.OverlayOpacity.Value;
_disabledApps = Settings.Properties.DisabledApps.Value;
@ -66,6 +67,7 @@ namespace Microsoft.PowerToys.Settings.UI.Library.ViewModels
private bool _isEnabled;
private int _themeIndex;
private bool _useLegacyPressWinKeyBehavior;
private int _pressTime;
private int _opacity;
@ -151,6 +153,42 @@ namespace Microsoft.PowerToys.Settings.UI.Library.ViewModels
}
}
public bool UseLegacyPressWinKeyBehavior
{
get
{
return _useLegacyPressWinKeyBehavior;
}
set
{
if (_useLegacyPressWinKeyBehavior != value)
{
_useLegacyPressWinKeyBehavior = value;
Settings.Properties.UseLegacyPressWinKeyBehavior.Value = value;
NotifyPropertyChanged();
}
}
}
public int PressTime
{
get
{
return _pressTime;
}
set
{
if (_pressTime != value)
{
_pressTime = value;
Settings.Properties.PressTime.Value = value;
NotifyPropertyChanged();
}
}
}
public string DisabledApps
{
get

View file

@ -615,6 +615,24 @@
<value>Press duration before showing (ms)</value>
<comment>pressing a key in milliseconds</comment>
</data>
<data name="ShortcutGuide_PressTime.Description" xml:space="preserve">
<value>How long to press the Windows key to activate the module</value>
</data>
<data name="ShortcutGuide_ActivationMethod.Header" xml:space="preserve">
<value>Activation method</value>
</data>
<data name="ShortcutGuide_ActivationMethod.Description" xml:space="preserve">
<value>Use a shortcut or press the Windows key for some time to activate</value>
</data>
<data name="Radio_ShortcutGuide_ActivationMethod_CustomizedShortcut.Content" xml:space="preserve">
<value>Customized shortcut</value>
</data>
<data name="Radio_ShortcutGuide_ActivationMethod_LongPressWindowsKey.Content" xml:space="preserve">
<value>Hold down Windows key</value>
</data>
<data name="ShortcutGuide_PressWinKeyWarning.Title" xml:space="preserve">
<value>In some edge cases Shortcut Guide might not function correctly when using this activation method</value>
</data>
<data name="ShortcutGuide_Appearance_Behavior.Header" xml:space="preserve">
<value>Appearance &amp; behavior</value>
</data>

View file

@ -12,6 +12,9 @@
<Page.Resources>
<converters:StringFormatConverter x:Key="StringFormatConverter"/>
<converters:BoolToObjectConverter x:Key="BoolToComboBoxIndexConverter" TrueValue="1" FalseValue="0"/>
<converters:BoolToVisibilityConverter x:Key="TrueToVisibleConverter" TrueValue="Visible" FalseValue="Collapsed"/>
<converters:BoolToVisibilityConverter x:Key="FalseToVisibleConverter" TrueValue="Collapsed" FalseValue="Visible"/>
</Page.Resources>
<controls:SettingsPageControl x:Uid="ShortcutGuide"
@ -29,12 +32,43 @@
<controls:SettingsGroup x:Uid="Shortcut" IsEnabled="{x:Bind Mode=OneWay, Path=ViewModel.IsEnabled}">
<controls:Setting x:Uid="Activation_Shortcut" Icon="&#xEDA7;" IsEnabled="{x:Bind Mode=OneWay, Path=ViewModel.IsEnabled}">
<controls:Setting x:Uid="ShortcutGuide_ActivationMethod">
<controls:Setting.ActionContent>
<ComboBox SelectedIndex="{x:Bind Mode=TwoWay, Path=ViewModel.UseLegacyPressWinKeyBehavior, Converter={StaticResource BoolToComboBoxIndexConverter}}" MinWidth="{StaticResource SettingActionControlMinWidth}">
<ComboBoxItem x:Uid="Radio_ShortcutGuide_ActivationMethod_CustomizedShortcut"/>
<ComboBoxItem x:Uid="Radio_ShortcutGuide_ActivationMethod_LongPressWindowsKey"/>
</ComboBox>
</controls:Setting.ActionContent>
</controls:Setting>
<controls:Setting x:Uid="Activation_Shortcut" Icon="&#xEDA7;" Visibility="{x:Bind Mode=OneWay, Path=ViewModel.UseLegacyPressWinKeyBehavior, Converter={StaticResource FalseToVisibleConverter}}">
<controls:Setting.ActionContent>
<controls:ShortcutControl HotkeySettings="{x:Bind Path=ViewModel.OpenShortcutGuide, Mode=TwoWay}"
MinWidth="{StaticResource SettingActionControlMinWidth}"/>
</controls:Setting.ActionContent>
</controls:Setting>
<controls:Setting x:Uid="ShortcutGuide_PressTime" Icon="&#xE916;" Visibility="{x:Bind Mode=OneWay, Path=ViewModel.UseLegacyPressWinKeyBehavior, Converter={StaticResource TrueToVisibleConverter}}">
<controls:Setting.ActionContent>
<muxc:NumberBox
Minimum="100"
Value="{x:Bind Mode=TwoWay, Path=ViewModel.PressTime}"
MinWidth="{StaticResource SettingActionControlMinWidth}"
SpinButtonPlacementMode="Compact"
HorizontalAlignment="Left"
SmallChange="50"
LargeChange="100"
/>
</controls:Setting.ActionContent>
</controls:Setting>
<muxc:InfoBar
x:Uid="ShortcutGuide_PressWinKeyWarning"
Severity="Warning"
IsOpen="{x:Bind Mode=OneWay, Path=ViewModel.UseLegacyPressWinKeyBehavior}"
IsClosable="False"
/>
</controls:SettingsGroup>
<controls:SettingsGroup x:Uid="ShortcutGuide_Appearance_Behavior" IsEnabled="{x:Bind Mode=OneWay, Path=ViewModel.IsEnabled}">