From 9f836ff3561911934c6d09deeb6b350d568420bb Mon Sep 17 00:00:00 2001 From: Serge <5920850+seerge@users.noreply.github.com> Date: Sun, 1 Oct 2023 11:22:20 +0200 Subject: [PATCH] Custom hotkey combos https://github.com/seerge/g-helper/issues/1386 --- app/Input/InputDispatcher.cs | 57 +++++++++++++++++++++++++++++------- app/Input/KeyboardHook.cs | 16 +++++----- 2 files changed, 54 insertions(+), 19 deletions(-) diff --git a/app/Input/InputDispatcher.cs b/app/Input/InputDispatcher.cs index 23f1d4e8..8565cb34 100644 --- a/app/Input/InputDispatcher.cs +++ b/app/Input/InputDispatcher.cs @@ -4,6 +4,7 @@ using GHelper.Mode; using Microsoft.Win32; using System.Diagnostics; using System.Management; +using System.Text.RegularExpressions; namespace GHelper.Input { @@ -142,25 +143,59 @@ namespace GHelper.Input } + + public static int[] ParseHexValues(string input) + { + string pattern = @"\b(0x[0-9A-Fa-f]{1,2}|[0-9A-Fa-f]{1,2})\b"; + + if (!Regex.IsMatch(input, $"^{pattern}(\\s+{pattern})*$")) return new int[0]; + + MatchCollection matches = Regex.Matches(input, pattern); + + int[] hexValues = new int[matches.Count]; + + for (int i = 0; i < matches.Count; i++) + { + string hexValueStr = matches[i].Value; + int hexValue = int.Parse(hexValueStr.StartsWith("0x", StringComparison.OrdinalIgnoreCase) + ? hexValueStr.Substring(2) + : hexValueStr, System.Globalization.NumberStyles.HexNumber); + + hexValues[i] = hexValue; + } + + return hexValues; + } + + static void CustomKey(string configKey = "m3") { string command = AppConfig.GetString(configKey + "_custom"); - int intKey; + int[] hexKeys = new int[0]; try { - intKey = Convert.ToInt32(command, 16); + hexKeys = ParseHexValues(command); } catch { - intKey = -1; } - - if (intKey > 0) - KeyboardHook.KeyPress((Keys)intKey); - else - LaunchProcess(command); + switch (hexKeys.Length) + { + case 1: + KeyboardHook.KeyPress((Keys)hexKeys[0]); + break; + case 2: + KeyboardHook.KeyKeyPress((Keys)hexKeys[0], (Keys)hexKeys[1]); + break; + case 3: + KeyboardHook.KeyKeyKeyPress((Keys)hexKeys[0], (Keys)hexKeys[1], (Keys)hexKeys[3]); + break; + default: + LaunchProcess(command); + break; + } } @@ -264,7 +299,7 @@ namespace GHelper.Input SetBrightness(+10); break; case Keys.F9: - KeyboardHook.KeyWinPress(Keys.P); + KeyboardHook.KeyKeyPress(Keys.LWin, Keys.P); break; case Keys.F10: HandleOptimizationEvent(107); @@ -273,7 +308,7 @@ namespace GHelper.Input HandleOptimizationEvent(108); break; case Keys.F12: - KeyboardHook.KeyWinPress(Keys.A); + KeyboardHook.KeyKeyPress(Keys.LWin, Keys.A); break; case Keys.VolumeDown: KeyProcess("m1"); @@ -442,7 +477,7 @@ namespace GHelper.Input static void ToggleTouchpad() { - KeyboardHook.KeyCtrlWinPress(Keys.F24); + KeyboardHook.KeyKeyKeyPress(Keys.ControlKey, Keys.LWin, Keys.F24); } public static void ToggleArrowLock() diff --git a/app/Input/KeyboardHook.cs b/app/Input/KeyboardHook.cs index 87ab9965..6f9ccdc3 100644 --- a/app/Input/KeyboardHook.cs +++ b/app/Input/KeyboardHook.cs @@ -26,23 +26,23 @@ public sealed class KeyboardHook : IDisposable keybd_event((byte)key, 0, KEYEVENTF_EXTENDEDKEY, IntPtr.Zero); } - public static void KeyWinPress(Keys key) + public static void KeyKeyPress(Keys key, Keys key2) { - keybd_event(VK_LWIN, 0, KEYEVENTF_EXTENDEDKEY, IntPtr.Zero); keybd_event((byte)key, 0, KEYEVENTF_EXTENDEDKEY, IntPtr.Zero); + keybd_event((byte)key2, 0, KEYEVENTF_EXTENDEDKEY, IntPtr.Zero); + keybd_event((byte)key2, 0, KEYEVENTF_EXTENDEDKEY | KEYEVENTF_KEYUP, IntPtr.Zero); keybd_event((byte)key, 0, KEYEVENTF_EXTENDEDKEY | KEYEVENTF_KEYUP, IntPtr.Zero); - keybd_event(VK_LWIN, 0, KEYEVENTF_EXTENDEDKEY | KEYEVENTF_KEYUP, IntPtr.Zero); } - public static void KeyCtrlWinPress(Keys key) + public static void KeyKeyKeyPress(Keys key, Keys key2, Keys key3) { - keybd_event(VK_LCONTROL, 0, KEYEVENTF_EXTENDEDKEY, IntPtr.Zero); - keybd_event(VK_LWIN, 0, KEYEVENTF_EXTENDEDKEY, IntPtr.Zero); keybd_event((byte)key, 0, KEYEVENTF_EXTENDEDKEY, IntPtr.Zero); + keybd_event((byte)key2, 0, KEYEVENTF_EXTENDEDKEY, IntPtr.Zero); + keybd_event((byte)key3, 0, KEYEVENTF_EXTENDEDKEY, IntPtr.Zero); + keybd_event((byte)key3, 0, KEYEVENTF_EXTENDEDKEY | KEYEVENTF_KEYUP, IntPtr.Zero); + keybd_event((byte)key2, 0, KEYEVENTF_EXTENDEDKEY | KEYEVENTF_KEYUP, IntPtr.Zero); keybd_event((byte)key, 0, KEYEVENTF_EXTENDEDKEY | KEYEVENTF_KEYUP, IntPtr.Zero); - keybd_event(VK_LWIN, 0, KEYEVENTF_EXTENDEDKEY | KEYEVENTF_KEYUP, IntPtr.Zero); - keybd_event(VK_LCONTROL, 0, KEYEVENTF_EXTENDEDKEY | KEYEVENTF_KEYUP, IntPtr.Zero); } ///