Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[MWB] Migrate to PowerToys-style shortcuts and disable Ctrlx3 for mul… #27442

Merged
merged 14 commits into from
Jul 26, 2023
Merged
6 changes: 6 additions & 0 deletions src/modules/MouseWithoutBorders/App/Class/Common.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
using System.Text;
using System.Threading;
using System.Windows.Forms;
using Microsoft.PowerToys.Settings.UI.Library;

// <summary>
// Most of the helper methods.
Expand Down Expand Up @@ -115,6 +116,11 @@ private Common()

internal static Process CurrentProcess { get; set; }

internal static bool HotkeyMatched(int vkCode, bool winDown, bool ctrlDown, bool altDown, bool shiftDown, HotkeySettings hotkey)
{
return !hotkey.IsEmpty() && (vkCode == hotkey.Code) && (!hotkey.Win || winDown) && (!hotkey.Alt || altDown) && (!hotkey.Shift || shiftDown) && (!hotkey.Ctrl || ctrlDown);
}

public static string BinaryName
{
get => Common.binaryName;
Expand Down
185 changes: 69 additions & 116 deletions src/modules/MouseWithoutBorders/App/Class/InputHook.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
using System.Reflection;
using System.Runtime.InteropServices;
using System.Windows.Forms;
using Microsoft.PowerToys.Settings.UI.Library;

[module: SuppressMessage("Microsoft.Design", "CA1031:DoNotCatchGeneralExceptionTypes", Scope = "member", Target = "MouseWithoutBorders.InputHook.#MouseHookProc(System.Int32,System.Int32,System.IntPtr)", Justification = "Dotnet port with style preservation")]
[module: SuppressMessage("Microsoft.Maintainability", "CA1502:AvoidExcessiveComplexity", Scope = "member", Target = "MouseWithoutBorders.InputHook.#ProcessKeyEx(System.Int32,System.Int32)", Justification = "Dotnet port with style preservation")]
Expand Down Expand Up @@ -364,17 +365,8 @@ private int KeyboardHookProc(int nCode, int wParam, IntPtr lParam)
}
}

// Returns false if we do not want to redirect the keystrokes to other machine(s).
private int ctrlTouchesDnIndex;

private int ctrlTouchesUpIndex = 1;
private const int CONTROL_TOUCH = 4;
private readonly long[] ctrlTouches = new long[CONTROL_TOUCH];

private bool ProcessKeyEx(int vkCode, int flags, KEYBDDATA hookCallbackKeybdData)
{
bool allTouched;

if ((flags & (int)Common.LLKHF.UP) == (int)Common.LLKHF.UP)
{
EasyMouseKeyDown = false;
Expand All @@ -389,13 +381,6 @@ private bool ProcessKeyEx(int vkCode, int flags, KEYBDDATA hookCallbackKeybdData
case VK.LCONTROL:
case VK.RCONTROL:
CtrlDown = false;

if (Setting.Values.HotKeySwitch2AllPC == 1)
{
ctrlTouches[ctrlTouchesUpIndex] = Common.GetTick();
ctrlTouchesUpIndex = ((ctrlTouchesUpIndex % CONTROL_TOUCH) + 2) % CONTROL_TOUCH;
}

break;

case VK.LMENU:
Expand Down Expand Up @@ -424,32 +409,7 @@ private bool ProcessKeyEx(int vkCode, int flags, KEYBDDATA hookCallbackKeybdData

case VK.LCONTROL:
case VK.RCONTROL:
Common.LogDebug("VK.RCONTROL");
CtrlDown = true;

if (Setting.Values.HotKeySwitch2AllPC == 1)
{
ctrlTouches[ctrlTouchesDnIndex] = Common.GetTick();
ctrlTouchesDnIndex = (ctrlTouchesDnIndex + 2) % CONTROL_TOUCH;
}

allTouched = true;

for (int i = 0; i < CONTROL_TOUCH; i++)
{
if (ctrlTouches[i] == 0 || Common.GetTick() - ctrlTouches[i] > 400)
{
allTouched = false;
break;
}
}

if (allTouched && Common.GetTick() - Common.JustGotAKey > 1000)
{
ResetLastSwitchKeys();
Common.SwitchToMultipleMode(Common.DesMachineID != ID.ALL, true);
}

break;

case VK.LMENU:
Expand Down Expand Up @@ -529,19 +489,83 @@ private void UpdateEasyMouseKeyDown(VK vkCode)

private static long lastHotKeyLockMachine;

private void ResetModifiersState(HotkeySettings matchingHotkey)
{
CtrlDown = CtrlDown && matchingHotkey.Ctrl;
altDown = altDown && matchingHotkey.Alt;
shiftDown = shiftDown && matchingHotkey.Shift;
winDown = winDown && matchingHotkey.Win;
}

private bool ProcessHotKeys(int vkCode, KEYBDDATA hookCallbackKeybdData)
{
if (vkCode == Setting.Values.HotKeyCaptureScreen && CtrlDown && shiftDown && !altDown &&
(Common.DesMachineID == Common.MachineID || Common.DesMachineID == ID.ALL))
if (Common.HotkeyMatched(vkCode, winDown, CtrlDown, altDown, shiftDown, Setting.Values.HotKeySwitch2AllPC))
{
CtrlDown = shiftDown = false;
ResetLastSwitchKeys();
Common.SwitchToMultipleMode(Common.DesMachineID != ID.ALL, true);
}

if (Common.HotkeyMatched(vkCode, winDown, CtrlDown, altDown, shiftDown, Setting.Values.HotKeyToggleEasyMouse))
{
if (!Common.RunOnLogonDesktop && !Common.RunOnScrSaverDesktop)
{
Common.PrepareScreenCapture();
EasyMouseOption easyMouseOption = (EasyMouseOption)Setting.Values.EasyMouse;

if (easyMouseOption is EasyMouseOption.Disable or EasyMouseOption.Enable)
{
Setting.Values.EasyMouse = (int)(easyMouseOption == EasyMouseOption.Disable ? EasyMouseOption.Enable : EasyMouseOption.Disable);

Common.ShowToolTip($"Easy Mouse has been toggled to [{(EasyMouseOption)Setting.Values.EasyMouse}] by a hotkey. You can change the hotkey in the Settings form.", 5000);
return false;
}
}
}
else if (Common.HotkeyMatched(vkCode, winDown, CtrlDown, altDown, shiftDown, Setting.Values.HotKeyLockMachine))
{
if (!Common.RunOnLogonDesktop
&& !Common.RunOnScrSaverDesktop)
{
if (Common.GetTick() - lastHotKeyLockMachine < 500)
{
Common.SwitchToMultipleMode(true, true);

hookCallbackKeybdData.wVk = (short)VK.LCONTROL;
KeyboardEvent(hookCallbackKeybdData);
hookCallbackKeybdData.wVk = (short)VK.LMENU;
KeyboardEvent(hookCallbackKeybdData);
hookCallbackKeybdData.wVk = vkCode;
KeyboardEvent(hookCallbackKeybdData);

hookCallbackKeybdData.dwFlags |= (int)Common.LLKHF.UP;

hookCallbackKeybdData.wVk = (short)VK.LCONTROL;
KeyboardEvent(hookCallbackKeybdData);
hookCallbackKeybdData.wVk = (short)VK.LMENU;
KeyboardEvent(hookCallbackKeybdData);
hookCallbackKeybdData.wVk = vkCode;
KeyboardEvent(hookCallbackKeybdData);

Common.SwitchToMultipleMode(false, true);

_ = NativeMethods.LockWorkStation();
}
else
{
KeyboardEvent(hookCallbackKeybdData);
}

lastHotKeyLockMachine = Common.GetTick();

return false;
}
}
else if (Common.HotkeyMatched(vkCode, winDown, CtrlDown, altDown, shiftDown, Setting.Values.HotKeyReconnect))
{
Common.ShowToolTip("Reconnecting...", 2000);
Common.LastReconnectByHotKeyTime = Common.GetTick();
Common.PleaseReopenSocket = Common.REOPEN_WHEN_HOTKEY;
return false;
}

if (CtrlDown && altDown)
{
Expand Down Expand Up @@ -578,72 +602,6 @@ private bool ProcessHotKeys(int vkCode, KEYBDDATA hookCallbackKeybdData)
return false;
}
}
else if (vkCode == Setting.Values.HotKeyLockMachine)
{
if (!Common.RunOnLogonDesktop
&& !Common.RunOnScrSaverDesktop)
{
if (Common.GetTick() - lastHotKeyLockMachine < 500)
{
Common.SwitchToMultipleMode(true, true);

hookCallbackKeybdData.wVk = (short)VK.LCONTROL;
KeyboardEvent(hookCallbackKeybdData);
hookCallbackKeybdData.wVk = (short)VK.LMENU;
KeyboardEvent(hookCallbackKeybdData);
hookCallbackKeybdData.wVk = vkCode;
KeyboardEvent(hookCallbackKeybdData);

hookCallbackKeybdData.dwFlags |= (int)Common.LLKHF.UP;

hookCallbackKeybdData.wVk = (short)VK.LCONTROL;
KeyboardEvent(hookCallbackKeybdData);
hookCallbackKeybdData.wVk = (short)VK.LMENU;
KeyboardEvent(hookCallbackKeybdData);
hookCallbackKeybdData.wVk = vkCode;
KeyboardEvent(hookCallbackKeybdData);

Common.SwitchToMultipleMode(false, true);

_ = NativeMethods.LockWorkStation();
}
else
{
KeyboardEvent(hookCallbackKeybdData);
}

lastHotKeyLockMachine = Common.GetTick();

return false;
}
}
else if (vkCode == Setting.Values.HotKeyReconnect)
{
Common.ShowToolTip("Reconnecting...", 2000);
Common.LastReconnectByHotKeyTime = Common.GetTick();
Common.PleaseReopenSocket = Common.REOPEN_WHEN_HOTKEY;
return false;
}
else if (vkCode == Setting.Values.HotKeySwitch2AllPC)
{
Common.SwitchToMultipleMode(Common.DesMachineID != ID.ALL, true);
return false;
}
else if (vkCode == Setting.Values.HotKeyToggleEasyMouse)
{
if (!Common.RunOnLogonDesktop && !Common.RunOnScrSaverDesktop)
{
EasyMouseOption easyMouseOption = (EasyMouseOption)Setting.Values.EasyMouse;

if (easyMouseOption is EasyMouseOption.Disable or EasyMouseOption.Enable)
{
Setting.Values.EasyMouse = (int)(easyMouseOption == EasyMouseOption.Disable ? EasyMouseOption.Enable : EasyMouseOption.Disable);

Common.ShowToolTip($"Easy Mouse has been toggled to [{(EasyMouseOption)Setting.Values.EasyMouse}] by a hotkey. You can change the hotkey in the Settings form.", 5000);
return false;
}
}
}
}

return true;
Expand Down Expand Up @@ -685,11 +643,6 @@ private static bool Switch2(int index)

internal void ResetLastSwitchKeys()
{
for (int i = 0; i < CONTROL_TOUCH; i++)
{
ctrlTouches[i] = 0;
}

CtrlDown = winDown = altDown = false;
}
}
Expand Down
30 changes: 13 additions & 17 deletions src/modules/MouseWithoutBorders/App/Class/InputSimulation.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
using System.Runtime.InteropServices;
using System.ServiceProcess;
using System.Threading.Tasks;
using Microsoft.PowerToys.Settings.UI.Library;
using Windows.UI.Input.Preview.Injection;
using static MouseWithoutBorders.Class.NativeMethods;

Expand Down Expand Up @@ -353,6 +354,14 @@ internal static void MouseClickDotForm(int x, int y)
private static bool altDown;
private static bool shiftDown;

private static void ResetModifiersState(HotkeySettings matchingHotkey)
{
ctrlDown = ctrlDown && matchingHotkey.Ctrl;
altDown = altDown && matchingHotkey.Alt;
shiftDown = shiftDown && matchingHotkey.Shift;
winDown = winDown && matchingHotkey.Win;
}

private static void InputProcessKeyEx(int vkCode, int flags, out bool eatKey)
{
eatKey = false;
Expand Down Expand Up @@ -387,28 +396,15 @@ private static void InputProcessKeyEx(int vkCode, int flags, out bool eatKey)
}
else
{
if (vkCode == Setting.Values.HotKeyLockMachine)
if (Common.HotkeyMatched(vkCode, winDown, ctrlDown, altDown, shiftDown, Setting.Values.HotKeyLockMachine))
{
if (!Common.RunOnLogonDesktop
&& !Common.RunOnScrSaverDesktop)
{
if (ctrlDown && altDown)
{
ctrlDown = altDown = false;
eatKey = true;
Common.ReleaseAllKeys();
_ = NativeMethods.LockWorkStation();
}
}
}
else if (vkCode == Setting.Values.HotKeyCaptureScreen && ctrlDown && shiftDown && !altDown)
{
ctrlDown = shiftDown = false;

if (!Common.RunOnLogonDesktop && !Common.RunOnScrSaverDesktop)
{
Common.PrepareScreenCapture();
ResetModifiersState(Setting.Values.HotKeyLockMachine);
eatKey = true;
Common.ReleaseAllKeys();
_ = NativeMethods.LockWorkStation();
}
}

Expand Down
Loading