[FindMyMouse]Support different activation modes (#28096)

* multiple activation modes for find my mouse

* use custom shortcut
This commit is contained in:
Davide Giacometti 2023-09-04 17:58:37 +02:00 committed by GitHub
parent 752c298ef3
commit 83f0625427
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
13 changed files with 222 additions and 36 deletions

View File

@ -2,6 +2,7 @@
//
#include "pch.h"
#include "FindMyMouse.h"
#include "WinHookEventIDs.h"
#include "trace.h"
#include "common/utils/game_mode.h"
#include "common/utils/process_path.h"
@ -246,6 +247,18 @@ LRESULT SuperSonar<D>::BaseWndProc(UINT message, WPARAM wParam, LPARAM lParam) n
return HTTRANSPARENT;
}
if (message == WM_PRIV_SHORTCUT)
{
if (m_sonarStart == NoSonar)
{
StartSonar();
}
else
{
StopSonar();
}
}
return DefWindowProc(m_hwnd, message, wParam, lParam);
}
@ -291,26 +304,27 @@ void SuperSonar<D>::OnSonarInput(WPARAM flags, HRAWINPUT hInput)
template<typename D>
void SuperSonar<D>::OnSonarKeyboardInput(RAWINPUT const& input)
{
if ( m_activationMethod != FindMyMouseActivationMethod::DoubleControlKey || input.data.keyboard.VKey != VK_CONTROL)
// Don't stop the sonar when the shortcut is released
if (m_activationMethod == FindMyMouseActivationMethod::Shortcut && (input.data.keyboard.Flags & RI_KEY_BREAK) != 0)
{
return;
}
if ((m_activationMethod != FindMyMouseActivationMethod::DoubleRightControlKey && m_activationMethod != FindMyMouseActivationMethod::DoubleLeftControlKey)
|| input.data.keyboard.VKey != VK_CONTROL)
{
StopSonar();
return;
}
bool pressed = (input.data.keyboard.Flags & RI_KEY_BREAK) == 0;
bool rightCtrl = (input.data.keyboard.Flags & RI_KEY_E0) != 0;
// Deal with rightCtrl first.
if (rightCtrl)
bool leftCtrlPressed = (input.data.keyboard.Flags & RI_KEY_E0) == 0;
bool rightCtrlPressed = (input.data.keyboard.Flags & RI_KEY_E0) != 0;
if ((m_activationMethod == FindMyMouseActivationMethod::DoubleRightControlKey && !rightCtrlPressed)
|| (m_activationMethod == FindMyMouseActivationMethod::DoubleLeftControlKey && !leftCtrlPressed))
{
/*
* SuperSonar originally exited when pressing right control after pressing left control twice.
* We take care of exiting FindMyMouse through module disabling in PowerToys settings instead.
if (m_sonarState == SonarState::ControlUp2)
{
Terminate();
}
*/
StopSonar();
return;
}
@ -644,6 +658,11 @@ struct CompositionSpotlight : SuperSonar<CompositionSpotlight>
}
}
HWND GetHwnd() noexcept
{
return m_hwnd;
}
private:
bool OnCompositionCreate()
try
@ -1057,6 +1076,8 @@ int FindMyMouseMain(HINSTANCE hinst, const FindMyMouseSettings& settings)
m_sonar = &sonar;
Logger::info("Initialized the sonar instance.");
InitializeWinhookEventIds();
MSG msg;
// Main message loop:
@ -1072,4 +1093,14 @@ int FindMyMouseMain(HINSTANCE hinst, const FindMyMouseSettings& settings)
return (int)msg.wParam;
}
HWND GetSonarHwnd() noexcept
{
if (m_sonar != nullptr)
{
return m_sonar->GetHwnd();
}
return nullptr;
}
#pragma endregion Super_Sonar_API

View File

@ -3,9 +3,11 @@
enum struct FindMyMouseActivationMethod : int
{
DoubleControlKey = 0,
ShakeMouse = 1,
EnumElements = 2, // number of elements in the enum, not counting this
DoubleLeftControlKey = 0,
DoubleRightControlKey = 1,
ShakeMouse = 2,
Shortcut = 3,
EnumElements = 4, // number of elements in the enum, not counting this
};
constexpr bool FIND_MY_MOUSE_DEFAULT_DO_NOT_ACTIVATE_ON_GAME_MODE = true;
@ -15,7 +17,7 @@ constexpr int FIND_MY_MOUSE_DEFAULT_OVERLAY_OPACITY = 50;
constexpr int FIND_MY_MOUSE_DEFAULT_SPOTLIGHT_RADIUS = 100;
constexpr int FIND_MY_MOUSE_DEFAULT_ANIMATION_DURATION_MS = 500;
constexpr int FIND_MY_MOUSE_DEFAULT_SPOTLIGHT_INITIAL_ZOOM = 9;
constexpr FindMyMouseActivationMethod FIND_MY_MOUSE_DEFAULT_ACTIVATION_METHOD = FindMyMouseActivationMethod::DoubleControlKey;
constexpr FindMyMouseActivationMethod FIND_MY_MOUSE_DEFAULT_ACTIVATION_METHOD = FindMyMouseActivationMethod::DoubleLeftControlKey;
constexpr int FIND_MY_MOUSE_DEFAULT_SHAKE_MINIMUM_DISTANCE = 1000;
struct FindMyMouseSettings
@ -36,3 +38,4 @@ int FindMyMouseMain(HINSTANCE hinst, const FindMyMouseSettings& settings);
void FindMyMouseDisable();
bool FindMyMouseIsEnabled();
void FindMyMouseApplySettings(const FindMyMouseSettings& settings);
HWND GetSonarHwnd() noexcept;

View File

@ -93,6 +93,7 @@
<ClInclude Include="pch.h" />
<ClInclude Include="resource.h" />
<ClInclude Include="trace.h" />
<ClInclude Include="WinHookEventIDs.h" />
</ItemGroup>
<ItemGroup>
<ClCompile Include="dllmain.cpp" />
@ -101,6 +102,7 @@
<PrecompiledHeader Condition="'$(CIBuild)'!='true'">Create</PrecompiledHeader>
</ClCompile>
<ClCompile Include="trace.cpp" />
<ClCompile Include="WinHookEventIDs.cpp" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\..\..\common\logger\logger.vcxproj">

View File

@ -30,6 +30,9 @@
<ClCompile Include="dllmain.cpp">
<Filter>Source Files</Filter>
</ClCompile>
<ClCompile Include="WinHookEventIDs.cpp">
<Filter>Source Files</Filter>
</ClCompile>
</ItemGroup>
<ItemGroup>
<ClInclude Include="pch.h">
@ -44,6 +47,9 @@
<ClInclude Include="resource.h">
<Filter>Resource Files</Filter>
</ClInclude>
<ClInclude Include="WinHookEventIDs.h">
<Filter>Header Files</Filter>
</ClInclude>
</ItemGroup>
<ItemGroup>
<ResourceCompile Include="FindMyMouse.rc">

View File

@ -0,0 +1,14 @@
#include "pch.h"
#include "WinHookEventIDs.h"
UINT WM_PRIV_SHORTCUT;
std::once_flag init_flag;
void InitializeWinhookEventIds()
{
std::call_once(init_flag, [&] {
WM_PRIV_SHORTCUT = RegisterWindowMessage(L"{1365FFC7-A44E-4171-9692-A3EEF378AE60}");
});
}

View File

@ -0,0 +1,5 @@
#pragma once
extern UINT WM_PRIV_SHORTCUT; // Shortcut is pressed
void InitializeWinhookEventIds();

View File

@ -3,6 +3,7 @@
#include <common/SettingsAPI/settings_objects.h>
#include "trace.h"
#include "FindMyMouse.h"
#include "WinHookEventIDs.h"
#include <thread>
#include <common/utils/logger_helper.h>
#include <common/utils/color.h>
@ -22,6 +23,7 @@ namespace
const wchar_t JSON_KEY_SPOTLIGHT_INITIAL_ZOOM[] = L"spotlight_initial_zoom";
const wchar_t JSON_KEY_EXCLUDED_APPS[] = L"excluded_apps";
const wchar_t JSON_KEY_SHAKING_MINIMUM_DISTANCE[] = L"shaking_minimum_distance";
const wchar_t JSON_KEY_ACTIVATION_SHORTCUT[] = L"activation_shortcut";
}
extern "C" IMAGE_DOS_HEADER __ImageBase;
@ -58,6 +60,9 @@ private:
// The PowerToy state.
bool m_enabled = false;
// Hotkey to invoke the module
HotkeyEx m_hotkey;
// Find My Mouse specific settings
FindMyMouseSettings m_findMyMouseSettings;
@ -157,6 +162,27 @@ public:
{
return m_enabled;
}
virtual std::optional<HotkeyEx> GetHotkeyEx() override
{
Logger::trace("GetHotkeyEx()");
if (m_findMyMouseSettings.activationMethod == FindMyMouseActivationMethod::Shortcut)
{
return m_hotkey;
}
return std::nullopt;
}
virtual void OnHotkeyEx() override
{
Logger::trace("OnHotkeyEx()");
HWND hwnd = GetSonarHwnd();
if (hwnd != nullptr)
{
PostMessageW(hwnd, WM_PRIV_SHORTCUT, NULL, NULL);
}
}
};
// Load the settings file.
@ -188,7 +214,15 @@ void FindMyMouse::parse_settings(PowerToysSettings::PowerToyValues& settings)
int value = static_cast<int>(jsonPropertiesObject.GetNamedNumber(JSON_KEY_VALUE));
if (value < static_cast<int>(FindMyMouseActivationMethod::EnumElements) && value >= 0)
{
findMyMouseSettings.activationMethod = static_cast<FindMyMouseActivationMethod>(value);
std::wstring version = (std::wstring)settingsObject.GetNamedString(L"version");
if (version == L"1.0" && value == 1)
{
findMyMouseSettings.activationMethod = FindMyMouseActivationMethod::ShakeMouse;
}
else
{
findMyMouseSettings.activationMethod = static_cast<FindMyMouseActivationMethod>(value);
}
}
else
{
@ -362,6 +396,46 @@ void FindMyMouse::parse_settings(PowerToysSettings::PowerToyValues& settings)
{
Logger::warn("Failed to initialize Shaking Minimum Distance from settings. Will use default value");
}
try
{
// Parse HotKey
auto jsonPropertiesObject = settingsObject.GetNamedObject(JSON_KEY_PROPERTIES).GetNamedObject(JSON_KEY_ACTIVATION_SHORTCUT);
auto hotkey = PowerToysSettings::HotkeyObject::from_json(jsonPropertiesObject);
m_hotkey = HotkeyEx();
if (hotkey.win_pressed())
{
m_hotkey.modifiersMask |= MOD_WIN;
}
if (hotkey.ctrl_pressed())
{
m_hotkey.modifiersMask |= MOD_CONTROL;
}
if (hotkey.shift_pressed())
{
m_hotkey.modifiersMask |= MOD_SHIFT;
}
if (hotkey.alt_pressed())
{
m_hotkey.modifiersMask |= MOD_ALT;
}
m_hotkey.vkCode = static_cast<WORD>(hotkey.get_code());
}
catch (...)
{
Logger::warn("Failed to initialize Activation Shortcut from settings. Will use default value");
}
if (!m_hotkey.modifiersMask)
{
Logger::info("Using default Activation Shortcut");
m_hotkey.modifiersMask = MOD_SHIFT | MOD_WIN;
m_hotkey.vkCode = 0x46; // F key
}
}
else
{

View File

@ -8,9 +8,14 @@ namespace Microsoft.PowerToys.Settings.UI.Library
{
public class FindMyMouseProperties
{
public HotkeySettings DefaultActivationShortcut => new HotkeySettings(true, false, false, true, 0x46);
[JsonPropertyName("activation_method")]
public IntProperty ActivationMethod { get; set; }
[JsonPropertyName("activation_shortcut")]
public HotkeySettings ActivationShortcut { get; set; }
[JsonPropertyName("do_not_activate_on_game_mode")]
public BoolProperty DoNotActivateOnGameMode { get; set; }
@ -41,6 +46,7 @@ namespace Microsoft.PowerToys.Settings.UI.Library
public FindMyMouseProperties()
{
ActivationMethod = new IntProperty(0);
ActivationShortcut = DefaultActivationShortcut;
DoNotActivateOnGameMode = new BoolProperty(true);
BackgroundColor = new StringProperty("#000000");
SpotlightColor = new StringProperty("#FFFFFF");

View File

@ -18,7 +18,7 @@ namespace Microsoft.PowerToys.Settings.UI.Library
{
Name = ModuleName;
Properties = new FindMyMouseProperties();
Version = "1.0";
Version = "1.1";
}
public string GetModuleName()
@ -29,6 +29,17 @@ namespace Microsoft.PowerToys.Settings.UI.Library
// This can be utilized in the future if the settings.json file is to be modified/deleted.
public bool UpgradeSettingsConfiguration()
{
if (Version == "1.0")
{
if (Properties.ActivationMethod.Value == 1)
{
Properties.ActivationMethod = new IntProperty(2);
}
Version = "1.1";
return true;
}
return false;
}
}

View File

@ -3,26 +3,20 @@
// See the LICENSE file in the project root for more information.
using System;
using System.Globalization;
using Microsoft.UI.Xaml;
using Microsoft.UI.Xaml.Data;
namespace Microsoft.PowerToys.Settings.UI.Converters
{
public sealed class FindMyMouseActivationShakeMouseIntToVisibilityConverter : IValueConverter
public sealed class FindMyMouseActivationIntToVisibilityConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, string language)
{
var activationShake = (int)value;
var selectedActivation = (int)value;
var expectedActivation = int.Parse(parameter as string, CultureInfo.InvariantCulture);
// Assumes 1 is the index for the shake mouse option in the activation method combo box
if (activationShake == 1)
{
return Visibility.Visible;
}
else
{
return Visibility.Collapsed;
}
return selectedActivation == expectedActivation ? Visibility.Visible : Visibility.Collapsed;
}
public object ConvertBack(object value, Type targetType, object parameter, string language)

View File

@ -11,7 +11,7 @@
AutomationProperties.LandmarkType="Main"
mc:Ignorable="d">
<Page.Resources>
<localConverters:FindMyMouseActivationShakeMouseIntToVisibilityConverter x:Key="FindMyMouseActivationShakeMouseIntToVisibilityConverter" />
<localConverters:FindMyMouseActivationIntToVisibilityConverter x:Key="FindMyMouseActivationIntToVisibilityConverter" />
</Page.Resources>
<controls:SettingsPageControl
x:Uid="MouseUtils"
@ -42,12 +42,14 @@
MinWidth="{StaticResource SettingActionControlMinWidth}"
SelectedIndex="{x:Bind Path=ViewModel.FindMyMouseActivationMethod, Mode=TwoWay}">
<ComboBoxItem x:Uid="MouseUtils_FindMyMouse_ActivationDoubleControlPress" />
<ComboBoxItem x:Uid="MouseUtils_FindMyMouse_ActivationDoubleRightControlPress" />
<ComboBoxItem x:Uid="MouseUtils_FindMyMouse_ActivationShakeMouse" />
<ComboBoxItem x:Uid="MouseUtils_FindMyMouse_ActivationCustomizedShortcut" />
</ComboBox>
<labs:SettingsExpander.Items>
<labs:SettingsCard
x:Uid="MouseUtils_FindMyMouse_ShakingMinimumDistance"
Visibility="{x:Bind Mode=OneWay, Path=ViewModel.FindMyMouseActivationMethod, Converter={StaticResource FindMyMouseActivationShakeMouseIntToVisibilityConverter}}">
Visibility="{x:Bind Mode=OneWay, Path=ViewModel.FindMyMouseActivationMethod, Converter={StaticResource FindMyMouseActivationIntToVisibilityConverter}, ConverterParameter=2}">
<NumberBox
MinWidth="{StaticResource SettingActionControlMinWidth}"
LargeChange="1000"
@ -57,6 +59,14 @@
SpinButtonPlacementMode="Compact"
Value="{x:Bind Mode=TwoWay, Path=ViewModel.FindMyMouseShakingMinimumDistance}" />
</labs:SettingsCard>
<labs:SettingsCard
x:Uid="MouseUtils_FindMyMouse_ActivationShortcut"
HeaderIcon="{ui:FontIcon FontFamily={StaticResource SymbolThemeFontFamily}, Glyph=&#xEDA7;}"
Visibility="{x:Bind Mode=OneWay, Path=ViewModel.FindMyMouseActivationMethod, Converter={StaticResource FindMyMouseActivationIntToVisibilityConverter}, ConverterParameter=3}">
<controls:ShortcutControl
MinWidth="{StaticResource SettingActionControlMinWidth}"
HotkeySettings="{x:Bind Path=ViewModel.FindMyMouseActivationShortcut, Mode=TwoWay}" />
</labs:SettingsCard>
<labs:SettingsCard ContentAlignment="Left">
<CheckBox
x:Uid="MouseUtils_Prevent_Activation_On_Game_Mode"

View File

@ -1,4 +1,4 @@
<?xml version="1.0" encoding="utf-8"?>
<?xml version="1.0" encoding="utf-8"?>
<root>
<!--
Microsoft ResX Schema
@ -1148,7 +1148,7 @@
<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>
<value>Custom shortcut</value>
</data>
<data name="Radio_ShortcutGuide_ActivationMethod_LongPressWindowsKey.Content" xml:space="preserve">
<value>Hold down Windows key</value>
@ -2384,7 +2384,7 @@ From there, simply click on one of the supported files in the File Explorer and
<comment>Mouse as in the hardware peripheral</comment>
</data>
<data name="Oobe_MouseUtils_FindMyMouse_Description.Text" xml:space="preserve">
<value>Shake the mouse or press the left Ctrl key twice to focus the mouse pointer.</value>
<value>Focus the mouse pointer pressing the Ctrl key twice, using a custom shortcut or shaking the mouse.</value>
<comment>Mouse as in the hardware peripheral. Key as in a keyboard key</comment>
</data>
<data name="Oobe_MouseUtils_MouseHighlighter.Text" xml:space="preserve">
@ -2512,7 +2512,7 @@ Right-click to remove the key combination, thereby deactivating the shortcut.</v
<comment>Refers to the utility name</comment>
</data>
<data name="MouseUtils_FindMyMouse.Description" xml:space="preserve">
<value>Find My Mouse highlights the position of the cursor when shaking the mouse or pressing the left Ctrl key twice.</value>
<value>Find My Mouse highlights the position of the cursor when pressing the Ctrl key twice, using a custom shortcut or when shaking the mouse.</value>
<comment>"Ctrl" is a keyboard key. "Find My Mouse" is the name of the utility</comment>
</data>
<data name="MouseUtils_Enable_FindMyMouse.Header" xml:space="preserve">
@ -3658,4 +3658,17 @@ Activate by holding the key for the character you want to add an accent to, then
<data name="AlwaysOnTop_FrameOpacity.Header" xml:space="preserve">
<value>Opacity (%)</value>
</data>
<data name="MouseUtils_FindMyMouse_ActivationCustomizedShortcut.Content" xml:space="preserve">
<value>Custom shortcut</value>
</data>
<data name="MouseUtils_FindMyMouse_ActivationDoubleRightControlPress.Content" xml:space="preserve">
<value>Press Right Control twice</value>
<comment>Right control is the physical key on the keyboard.</comment>
</data>
<data name="MouseUtils_FindMyMouse_ActivationShortcut.Description" xml:space="preserve">
<value>Customize the shortcut to turn on or off this mode</value>
</data>
<data name="MouseUtils_FindMyMouse_ActivationShortcut.Header" xml:space="preserve">
<value>Activation shortcut</value>
</data>
</root>

View File

@ -48,7 +48,7 @@ namespace Microsoft.PowerToys.Settings.UI.ViewModels
}
FindMyMouseSettingsConfig = findMyMouseSettingsRepository.SettingsConfig;
_findMyMouseActivationMethod = FindMyMouseSettingsConfig.Properties.ActivationMethod.Value < 2 ? FindMyMouseSettingsConfig.Properties.ActivationMethod.Value : 0;
_findMyMouseActivationMethod = FindMyMouseSettingsConfig.Properties.ActivationMethod.Value < 4 ? FindMyMouseSettingsConfig.Properties.ActivationMethod.Value : 0;
_findMyMouseDoNotActivateOnGameMode = FindMyMouseSettingsConfig.Properties.DoNotActivateOnGameMode.Value;
string backgroundColor = FindMyMouseSettingsConfig.Properties.BackgroundColor.Value;
@ -218,6 +218,23 @@ namespace Microsoft.PowerToys.Settings.UI.ViewModels
}
}
public HotkeySettings FindMyMouseActivationShortcut
{
get
{
return FindMyMouseSettingsConfig.Properties.ActivationShortcut;
}
set
{
if (FindMyMouseSettingsConfig.Properties.ActivationShortcut != value)
{
FindMyMouseSettingsConfig.Properties.ActivationShortcut = value ?? FindMyMouseSettingsConfig.Properties.DefaultActivationShortcut;
NotifyFindMyMousePropertyChanged();
}
}
}
public bool FindMyMouseDoNotActivateOnGameMode
{
get