[Keyboard Manager] Remove service enable/disable separate from module, fix editor clear shortcut (#46530)

Two changes to shortcuts here:
1) Remove toggling the KBM service with a shortcut or via command
palette
2) Ensure that shortcut is disabled for editor when shortcut is cleared
This commit is contained in:
Zach Teutsch
2026-03-26 13:20:50 -04:00
committed by GitHub
parent c05ba4e2c8
commit dc533fbdb3
15 changed files with 27 additions and 245 deletions

View File

@@ -300,10 +300,6 @@ namespace winrt::PowerToys::Interop::implementation
{
return CommonSharedConstants::OPEN_NEW_KEYBOARD_MANAGER_EVENT;
}
hstring Constants::ToggleKeyboardManagerActiveEvent()
{
return CommonSharedConstants::TOGGLE_KEYBOARD_MANAGER_ACTIVE_EVENT;
}
hstring Constants::KeyboardManagerEngineInstanceMutex()
{
return CommonSharedConstants::KEYBOARD_MANAGER_ENGINE_INSTANCE_MUTEX;

View File

@@ -78,7 +78,6 @@ namespace winrt::PowerToys::Interop::implementation
static hstring MWBToggleEasyMouseEvent();
static hstring MWBReconnectEvent();
static hstring OpenNewKeyboardManagerEvent();
static hstring ToggleKeyboardManagerActiveEvent();
static hstring KeyboardManagerEngineInstanceMutex();
};
}

View File

@@ -75,7 +75,6 @@ namespace PowerToys
static String MWBToggleEasyMouseEvent();
static String MWBReconnectEvent();
static String OpenNewKeyboardManagerEvent();
static String ToggleKeyboardManagerActiveEvent();
static String KeyboardManagerEngineInstanceMutex();
}
}

View File

@@ -173,7 +173,6 @@ namespace CommonSharedConstants
// Path to events used by Keyboard Manager
const wchar_t OPEN_NEW_KEYBOARD_MANAGER_EVENT[] = L"Local\\PowerToysOpenNewKeyboardManagerEvent-9c1d2e3f-4b5a-6c7d-8e9f-0a1b2c3d4e5f";
const wchar_t TOGGLE_KEYBOARD_MANAGER_ACTIVE_EVENT[] = L"Local\\PowerToysToggleKeyboardManagerActiveEvent-7f3a1d5c-2e94-4ff4-8b6a-90fd2bc4d2a7";
const wchar_t KEYBOARD_MANAGER_ENGINE_INSTANCE_MUTEX[] = L"Local\\PowerToys_KBMEngine_InstanceMutex";
// used from quick access window

View File

@@ -1,24 +0,0 @@
// 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 Microsoft.CommandPalette.Extensions.Toolkit;
using PowerToysExtension.Helpers;
using PowerToysExtension.Properties;
namespace PowerToysExtension.Commands;
internal sealed partial class ToggleKeyboardManagerListeningCommand : InvokableCommand
{
public ToggleKeyboardManagerListeningCommand()
{
Name = "Toggle Keyboard Manager active state";
}
public override CommandResult Invoke()
{
return KeyboardManagerStateService.TryToggleListening()
? CommandResult.KeepOpen()
: CommandResult.ShowToast(Resources.ResourceManager.GetString("KeyboardManager_ToggleListening_Error", Resources.Culture) ?? "Keyboard Manager is unavailable. Try enabling it in PowerToys settings.");
}
}

View File

@@ -43,21 +43,6 @@ internal static class KeyboardManagerStateService
return false;
}
internal static bool TryToggleListening()
{
try
{
using var evt = EventWaitHandle.OpenExisting(Constants.ToggleKeyboardManagerActiveEvent());
var signaled = evt.Set();
PollStatus();
return signaled;
}
catch
{
return false;
}
}
private static void PollStatus()
{
var isListening = IsListening();

View File

@@ -14,11 +14,6 @@ internal static class PowerToysResourcesHelper
internal static IconInfo IconFromSettingsIcon(string fileName) => IconHelpers.FromRelativePath($"{SettingsIconRoot}{fileName}");
internal static IconInfo KeyboardManagerListeningIcon(bool isListening) => IconHelpers.FromRelativePath(
isListening
? $"{AssetsRoot}KeyboardManager\\KeyboardManagerListeningOn.svg"
: $"{AssetsRoot}KeyboardManager\\KeyboardManagerListeningOff.svg");
#if DEBUG
public static IconInfo ProviderIcon() => IconFromSettingsIcon("PowerToys.dark.png");
#else

View File

@@ -22,19 +22,6 @@ internal sealed class KeyboardManagerModuleCommandProvider : ModuleCommandProvid
var title = module.ModuleDisplayName();
var icon = module.ModuleIcon();
if (ModuleEnablementService.IsModuleEnabled(module))
{
var isListening = KeyboardManagerStateService.IsListening();
yield return new ListItem(new ToggleKeyboardManagerListeningCommand() { Id = "com.microsoft.powertoys.keyboardManager.toggleListening" })
{
Title = GetResourceString("KeyboardManager_ToggleListening_Title", "Keyboard Manager: Toggle active state"),
Subtitle = isListening
? GetResourceString("KeyboardManager_ToggleListening_On_Subtitle", "Keyboard Manager is active. Invoke to stop listening.")
: GetResourceString("KeyboardManager_ToggleListening_Off_Subtitle", "Keyboard Manager is paused. Invoke to start listening."),
Icon = PowerToysResourcesHelper.KeyboardManagerListeningIcon(isListening),
};
}
if (IsUseNewEditorEnabled())
{
yield return new ListItem(new OpenNewKeyboardManagerEditorCommand() { Id = "com.microsoft.powertoys.keyboardManager.openNewEditor" })

View File

@@ -414,18 +414,6 @@
<data name="KeyboardManager_OpenNewEditor_Subtitle" xml:space="preserve">
<value>Open the Keyboard Manager remap editor</value>
</data>
<data name="KeyboardManager_ToggleListening_Title" xml:space="preserve">
<value>Keyboard Manager: Toggle active state</value>
</data>
<data name="KeyboardManager_ToggleListening_On_Subtitle" xml:space="preserve">
<value>Keyboard Manager is active. Invoke to stop listening.</value>
</data>
<data name="KeyboardManager_ToggleListening_Off_Subtitle" xml:space="preserve">
<value>Keyboard Manager is paused. Invoke to start listening.</value>
</data>
<data name="KeyboardManager_ToggleListening_Error" xml:space="preserve">
<value>Keyboard Manager is unavailable. Try enabling it in PowerToys settings.</value>
</data>
<!-- Light Switch Module -->
<data name="LightSwitch_Toggle_Title" xml:space="preserve">
<value>Light Switch: Toggle theme</value>

View File

@@ -38,7 +38,6 @@ namespace
const wchar_t JSON_KEY_CTRL[] = L"ctrl";
const wchar_t JSON_KEY_SHIFT[] = L"shift";
const wchar_t JSON_KEY_CODE[] = L"code";
const wchar_t JSON_KEY_ACTIVATION_SHORTCUT[] = L"ToggleShortcut";
const wchar_t JSON_KEY_EDITOR_SHORTCUT[] = L"EditorShortcut";
const wchar_t JSON_KEY_USE_NEW_EDITOR[] = L"useNewEditor";
}
@@ -57,25 +56,21 @@ private:
//contains the non localized key of the powertoy
std::wstring app_key = KeyboardManagerConstants::ModuleName;
// Hotkey for toggling the module
Hotkey m_hotkey = { .key = 0 };
// Hotkey for opening the editor
Hotkey m_editorHotkey = { .key = 0 };
// Whether to use the new WinUI3 editor
bool m_useNewEditor = false;
ULONGLONG m_lastHotkeyToggleTime = 0;
ULONGLONG m_lastHotkeyTime = 0;
HANDLE m_hProcess = nullptr;
HANDLE m_hEditorProcess = nullptr;
HANDLE m_hTerminateEngineEvent = nullptr;
HANDLE m_open_new_editor_event_handle{ nullptr };
HANDLE m_toggle_active_event_handle{ nullptr };
std::thread m_toggle_thread;
std::atomic<bool> m_toggle_thread_running{ false };
std::thread m_editor_listener_thread;
std::atomic<bool> m_editor_listener_running{ false };
void refresh_process_state()
@@ -88,19 +83,6 @@ private:
}
}
void toggle_engine()
{
refresh_process_state();
if (m_active)
{
stop_engine();
}
else
{
start_engine();
}
}
bool start_engine()
{
refresh_process_state();
@@ -176,35 +158,9 @@ private:
void parse_hotkey(PowerToysSettings::PowerToyValues& settings)
{
auto settingsObject = settings.get_raw_json();
if (settingsObject.GetView().Size())
{
try
{
auto jsonHotkeyObject = settingsObject.GetNamedObject(JSON_KEY_PROPERTIES)
.GetNamedObject(JSON_KEY_ACTIVATION_SHORTCUT);
m_hotkey.win = jsonHotkeyObject.GetNamedBoolean(JSON_KEY_WIN);
m_hotkey.alt = jsonHotkeyObject.GetNamedBoolean(JSON_KEY_ALT);
m_hotkey.shift = jsonHotkeyObject.GetNamedBoolean(JSON_KEY_SHIFT);
m_hotkey.ctrl = jsonHotkeyObject.GetNamedBoolean(JSON_KEY_CTRL);
m_hotkey.key = static_cast<unsigned char>(jsonHotkeyObject.GetNamedNumber(JSON_KEY_CODE));
}
catch (...)
{
Logger::error("Failed to initialize Keyboard Manager toggle shortcut");
}
}
if (!m_hotkey.key)
{
// Set default: Win+Shift+K
m_hotkey.win = true;
m_hotkey.shift = true;
m_hotkey.ctrl = false;
m_hotkey.alt = false;
m_hotkey.key = 'K';
}
// Parse editor shortcut
bool editorShortcutParsed = false;
if (settingsObject.GetView().Size())
{
try
@@ -216,6 +172,7 @@ private:
m_editorHotkey.shift = jsonEditorHotkeyObject.GetNamedBoolean(JSON_KEY_SHIFT);
m_editorHotkey.ctrl = jsonEditorHotkeyObject.GetNamedBoolean(JSON_KEY_CTRL);
m_editorHotkey.key = static_cast<unsigned char>(jsonEditorHotkeyObject.GetNamedNumber(JSON_KEY_CODE));
editorShortcutParsed = true;
}
catch (...)
{
@@ -223,9 +180,9 @@ private:
}
}
if (!m_editorHotkey.key)
if (!editorShortcutParsed && !m_editorHotkey.key)
{
// Set default: Win+Shift+Q
// Set default: Win+Shift+Q (only when no setting exists)
m_editorHotkey.win = true;
m_editorHotkey.shift = true;
m_editorHotkey.ctrl = false;
@@ -287,7 +244,6 @@ public:
}
m_open_new_editor_event_handle = CreateDefaultEvent(CommonSharedConstants::OPEN_NEW_KEYBOARD_MANAGER_EVENT);
m_toggle_active_event_handle = CreateDefaultEvent(CommonSharedConstants::TOGGLE_KEYBOARD_MANAGER_ACTIVE_EVENT);
init_settings();
};
@@ -306,11 +262,6 @@ public:
CloseHandle(m_open_new_editor_event_handle);
m_open_new_editor_event_handle = nullptr;
}
if (m_toggle_active_event_handle)
{
CloseHandle(m_toggle_active_event_handle);
m_toggle_active_event_handle = nullptr;
}
if (m_hEditorProcess)
{
CloseHandle(m_hEditorProcess);
@@ -412,22 +363,12 @@ public:
return false;
}
// Return the invocation hotkeys for toggling and opening the editor
// Return the invocation hotkey for opening the editor
virtual size_t get_hotkeys(Hotkey* hotkeys, size_t buffer_size) override
{
size_t count = 0;
// Hotkey 0: toggle engine
if (m_hotkey.key)
{
if (hotkeys && buffer_size > count)
{
hotkeys[count] = m_hotkey;
}
count++;
}
// Hotkey 1: open editor (only when using new editor)
// Hotkey 0: open editor (only when using new editor)
if (m_useNewEditor && m_editorHotkey.key)
{
if (hotkeys && buffer_size > count)
@@ -442,69 +383,44 @@ public:
void StartOpenEditorListener()
{
if (m_toggle_thread_running || (!m_open_new_editor_event_handle && !m_toggle_active_event_handle))
if (m_editor_listener_running || !m_open_new_editor_event_handle)
{
return;
}
m_toggle_thread_running = true;
m_toggle_thread = std::thread([this]() {
HANDLE handles[2]{};
DWORD handle_count = 0;
DWORD open_editor_index = MAXDWORD;
DWORD toggle_active_index = MAXDWORD;
if (m_open_new_editor_event_handle)
m_editor_listener_running = true;
m_editor_listener_thread = std::thread([this]() {
while (m_editor_listener_running)
{
open_editor_index = handle_count;
handles[handle_count++] = m_open_new_editor_event_handle;
}
if (m_toggle_active_event_handle)
{
toggle_active_index = handle_count;
handles[handle_count++] = m_toggle_active_event_handle;
}
while (m_toggle_thread_running)
{
const DWORD wait_result = WaitForMultipleObjects(handle_count, handles, FALSE, 500);
if (!m_toggle_thread_running)
const DWORD wait_result = WaitForSingleObject(m_open_new_editor_event_handle, 500);
if (!m_editor_listener_running)
{
break;
}
if (open_editor_index != MAXDWORD && wait_result == (WAIT_OBJECT_0 + open_editor_index))
if (wait_result == WAIT_OBJECT_0)
{
launch_editor();
}
else if (toggle_active_index != MAXDWORD && wait_result == (WAIT_OBJECT_0 + toggle_active_index))
{
toggle_engine();
}
}
});
}
void StopOpenEditorListener()
{
if (!m_toggle_thread_running)
if (!m_editor_listener_running)
{
return;
}
m_toggle_thread_running = false;
m_editor_listener_running = false;
if (m_open_new_editor_event_handle)
{
SetEvent(m_open_new_editor_event_handle);
}
if (m_toggle_active_event_handle)
if (m_editor_listener_thread.joinable())
{
SetEvent(m_toggle_active_event_handle);
}
if (m_toggle_thread.joinable())
{
m_toggle_thread.join();
m_editor_listener_thread.join();
}
}
@@ -584,22 +500,17 @@ public:
return false;
}
constexpr ULONGLONG hotkeyToggleDebounceMs = 500;
constexpr ULONGLONG hotkeyDebounceMs = 500;
const auto now = GetTickCount64();
if (now - m_lastHotkeyToggleTime < hotkeyToggleDebounceMs)
if (now - m_lastHotkeyTime < hotkeyDebounceMs)
{
return true;
}
m_lastHotkeyToggleTime = now;
m_lastHotkeyTime = now;
if (hotkeyId == 0)
{
// Toggle engine on/off
toggle_engine();
}
else if (hotkeyId == 1)
{
// Open the new editor (only in new editor mode)
// Open the editor
launch_editor();
}

View File

@@ -21,20 +21,15 @@ namespace Microsoft.PowerToys.Settings.UI.Library
[CmdConfigureIgnoreAttribute]
public GenericProperty<List<string>> KeyboardConfigurations { get; set; }
public HotkeySettings DefaultToggleShortcut => new HotkeySettings(true, false, false, true, 0x4B);
public HotkeySettings DefaultEditorShortcut => new HotkeySettings(true, false, false, true, 0x51);
public KeyboardManagerProperties()
{
ToggleShortcut = DefaultToggleShortcut;
EditorShortcut = DefaultEditorShortcut;
KeyboardConfigurations = new GenericProperty<List<string>>(new List<string> { "default", });
ActiveConfiguration = new GenericProperty<string>("default");
}
public HotkeySettings ToggleShortcut { get; set; }
public HotkeySettings EditorShortcut { get; set; }
[JsonPropertyName("useNewEditor")]

View File

@@ -38,10 +38,6 @@ namespace Microsoft.PowerToys.Settings.UI.Library
{
var hotkeyAccessors = new List<HotkeyAccessor>
{
new HotkeyAccessor(
() => Properties.ToggleShortcut,
value => Properties.ToggleShortcut = value ?? Properties.DefaultToggleShortcut,
"Toggle_Shortcut"),
new HotkeyAccessor(
() => Properties.EditorShortcut,
value => Properties.EditorShortcut = value ?? Properties.DefaultEditorShortcut,

View File

@@ -62,28 +62,12 @@
<controls:SettingsPageControl.ModuleContent>
<StackPanel ChildrenTransitions="{StaticResource SettingsCardsAnimations}" Orientation="Vertical">
<controls:GPOInfoControl ShowWarning="{x:Bind ViewModel.IsEnabledGpoConfigured, Mode=OneWay}">
<tkcontrols:SettingsExpander
<tkcontrols:SettingsCard
Name="KeyboardManagerEnableToggle"
x:Uid="KeyboardManager_EnableToggle"
HeaderIcon="{ui:BitmapIcon Source=/Assets/Settings/Icons/KeyboardManager.png}">
<ToggleSwitch x:Uid="ToggleSwitch" IsOn="{x:Bind ViewModel.Enabled, Mode=TwoWay}" />
<tkcontrols:SettingsExpander.Description>
<HyperlinkButton NavigateUri="https://aka.ms/powerToysCannotRemapKeys">
<TextBlock x:Uid="KBM_KeysCannotBeRemapped" FontWeight="SemiBold" />
</HyperlinkButton>
</tkcontrols:SettingsExpander.Description>
<tkcontrols:SettingsExpander.Items>
<tkcontrols:SettingsCard
Name="ToggleShortcut"
x:Uid="KeyboardManager_Toggle_Shortcut"
IsEnabled="{x:Bind ViewModel.Enabled, Mode=OneWay}">
<controls:ShortcutControl
MinWidth="{StaticResource SettingActionControlMinWidth}"
AllowDisable="True"
HotkeySettings="{x:Bind Path=ViewModel.ToggleShortcut, Mode=TwoWay}" />
</tkcontrols:SettingsCard>
</tkcontrols:SettingsExpander.Items>
</tkcontrols:SettingsExpander>
</tkcontrols:SettingsCard>
</controls:GPOInfoControl>
<tkcontrols:SwitchPresenter TargetType="x:Boolean" Value="{x:Bind ViewModel.UseNewEditor, Mode=OneWay}">

View File

@@ -1909,12 +1909,6 @@ Made with 💗 by Microsoft and the PowerToys community.</value>
</data>
<data name="Activation_Shortcut.Description" xml:space="preserve">
<value>Customize the shortcut to activate this module</value>
</data>
<data name="KeyboardManager_Toggle_Shortcut.Header" xml:space="preserve">
<value>Shortcut</value>
</data>
<data name="KeyboardManager_Toggle_Shortcut.Description" xml:space="preserve">
<value>Enable or disable this module (Note: the Settings UI will not update)</value>
</data>
<data name="KeyboardManager_Editor_Shortcut.Header" xml:space="preserve">
<value>Editor shortcut</value>

View File

@@ -181,34 +181,12 @@ namespace Microsoft.PowerToys.Settings.UI.ViewModels
{
var hotkeysDict = new Dictionary<string, HotkeySettings[]>
{
[ModuleName] = [ToggleShortcut, EditorShortcut],
[ModuleName] = [EditorShortcut],
};
return hotkeysDict;
}
public HotkeySettings ToggleShortcut
{
get => Settings.Properties.ToggleShortcut;
set
{
if (value != Settings.Properties.ToggleShortcut)
{
Settings.Properties.ToggleShortcut = value == null ? Settings.Properties.DefaultToggleShortcut : value;
OnPropertyChanged(nameof(ToggleShortcut));
NotifySettingsChanged();
SendConfigMSG(
string.Format(
CultureInfo.InvariantCulture,
"{{ \"powertoys\": {{ \"{0}\": {1} }} }}",
KeyboardManagerSettings.ModuleName,
JsonSerializer.Serialize(Settings, SourceGenerationContextContext.Default.KeyboardManagerSettings)));
}
}
}
public bool UseNewEditor
{
get => Settings.Properties.UseNewEditor;