From 08326809d6571dd3cad3043f805de548fe5540df Mon Sep 17 00:00:00 2001 From: Jeremy Date: Thu, 26 Dec 2024 14:25:18 +1100 Subject: [PATCH] cleanup --- ChefKeys/ChefKeys.cs | 71 ++++++++------------------------------------ 1 file changed, 13 insertions(+), 58 deletions(-) diff --git a/ChefKeys/ChefKeys.cs b/ChefKeys/ChefKeys.cs index a2f64e8..3cf73d2 100644 --- a/ChefKeys/ChefKeys.cs +++ b/ChefKeys/ChefKeys.cs @@ -1,13 +1,9 @@ using System; using System.Collections.Generic; using System.Diagnostics; -using System.Globalization; using System.Linq; -using System.Reflection.Emit; using System.Runtime.InteropServices; -using System.Windows.Controls; using System.Windows.Input; -using static System.Net.Mime.MediaTypeNames; namespace ChefKeys { @@ -27,11 +23,7 @@ public static class ChefKeysManager private const int VK_LSHIFT = 0xA0; private const int VK_RSHIFT = 0xA1; private const int VK_SHIFT = 0x10; - private const int VK_Z = 0x5A; private const uint KEYEVENTF_KEYUP = 0x0002; - private const int VK_CONTROL = 0x11; - private const int VK_ALT = 0x12; - private const int VK_WIN = 91; private static LowLevelKeyboardProc _proc; private static IntPtr _hookID = IntPtr.Zero; @@ -53,9 +45,6 @@ public static class ChefKeysManager [DllImport("user32.dll")] private static extern void keybd_event(byte bVk, byte bScan, uint dwFlags, UIntPtr dwExtraInfo); - [DllImport("user32.dll", CharSet = CharSet.Auto, ExactSpelling = true, CallingConvention = CallingConvention.Winapi)] - private static extern short GetKeyState(int keyCode); - [DllImport("user32.dll")] public static extern short GetAsyncKeyState(int vKey); @@ -78,15 +67,17 @@ internal void RegisterKeyCombo(string hotkey, int vk_code, Action action if (KeyComboRecords.Any(x => x.comboRaw == hotkey)) return; - KeyComboRecords.Add(new KeyComboRecord - { - vk_code = vk_code, - vkCodeCombo0 = vkCodeCombo0, - vkCodeCombo1 = vkCodeCombo1, - vkCodeCombo2 = vkCodeCombo2, - action = action, - comboRaw = hotkey - }); + KeyComboRecords + .Add( + new KeyComboRecord + { + vk_code = vk_code, + vkCodeCombo0 = vkCodeCombo0, + vkCodeCombo1 = vkCodeCombo1, + vkCodeCombo2 = vkCodeCombo2, + action = action, + comboRaw = hotkey + }); } }; @@ -173,7 +164,6 @@ private bool NonRegisteredModifierKeyPressed(Dictionary comboKeys) } private static readonly Dictionary registeredHotkeys; - private static readonly KeyPressActionRecord nonRegisteredKeyRecord; private static bool isLWinKeyDown = false; @@ -183,39 +173,9 @@ private bool NonRegisteredModifierKeyPressed(Dictionary comboKeys) static ChefKeysManager() { - //var registeredCombos = new List() - //{ - // new KeyComboRecord { vkCodeCombo0 = VK_LALT, comboRaw = "LAlt+Z" }, - // new KeyComboRecord { vkCodeCombo0 = VK_LSHIFT, comboRaw = "LShift+Z" }, - //}; - - //registeredHotkeys = new Dictionary() - //{ - //{ VK_LCTRL, new KeyPressActionRecord {vk_code = VK_LCTRL, HandleKeyPress = HandleRegisteredKeyPress, vkCodeCombo0 = VK_LSHIFT, vkCodeCombo1 = VK_LALT, vkCodeCombo2 = VK_Z, isSingleKeyRegistered = true, isComboKeyRegistered = true } }, - //{ 91, new KeyPressActionRecord {vk_code = 91, HandleKeyPress = HandleRegisteredKeyPress, isSingleKeyRegistered = true } }, - - - //{ VK_Z, new KeyPressActionRecord {vk_code = VK_Z, HandleKeyPress = HandleRegisteredKeyPress, KeyComboRecords = registeredCombos, isSingleKeyRegistered = true, isComboKeyRegistered = true} } - - - - //{ VK_LCTRL, new KeyPressActionRecord {vk_code = VK_LCTRL, HandleKeyPress = HandleRegisteredKeyPress, isSingleKeyRegistered = true } }, - - //}; - - //registeredHotkeysCombo = new Dictionary() - //{ - // { VK_Z, new KeyPressActionRecord{ vk_code = VK_Z, HandleKeyPress = HandleRegisteredComboKeyPress, vkCodeCombo0 = VK_LSHIFT, vkCodeCombo1 = VK_LALT, vkCodeCombo2 = VK_LCTRL } }, - // { VK_LCTRL, new KeyPressActionRecord{ vk_code = VK_LCTRL, HandleKeyPress = HandleRegisteredComboKeyPress, vkCodeCombo0 = VK_LALT, vkCodeCombo1 = VK_LSHIFT, vkCodeCombo2 = VK_Z } }, - // { VK_LALT, new KeyPressActionRecord{ vk_code = VK_LALT, HandleKeyPress = HandleRegisteredComboKeyPress, vkCodeCombo0 = VK_LCTRL, vkCodeCombo1 = VK_LSHIFT, vkCodeCombo2 = VK_Z } }, - // { VK_LSHIFT, new KeyPressActionRecord{ vk_code = VK_LSHIFT, HandleKeyPress = HandleRegisteredComboKeyPress, vkCodeCombo0 = VK_LALT, vkCodeCombo1 = VK_LCTRL, vkCodeCombo2 = VK_Z } } - //}; - registeredHotkeys = new Dictionary(); nonRegisteredKeyRecord = new KeyPressActionRecord { vk_code = 0, HandleKeyPress = HandleNonRegisteredKeyPress }; - - _proc = HookCallback; } @@ -240,12 +200,9 @@ private static IntPtr SetHook(LowLevelKeyboardProc proc) private delegate IntPtr LowLevelKeyboardProc(int nCode, IntPtr wParam, IntPtr lParam); - private static IEnumerable SplitHotkeyReversed(string hotkeys) => hotkeys.Split("+", StringSplitOptions.RemoveEmptyEntries).Reverse(); + private static IEnumerable SplitHotkeyReversed(string hotkeys) => hotkeys.Split("+", StringSplitOptions.RemoveEmptyEntries).Reverse(); - public static void RegisterHotkey(string hotkeys, Action action) - { - RegisterHotkey(hotkeys, hotkeys, action); - } + public static void RegisterHotkey(string hotkeys, Action action) => RegisterHotkey(hotkeys, hotkeys, action); public static void RegisterHotkey(string hotkeys, string previousHotkey, Action action) { @@ -471,8 +428,6 @@ private static bool HandleNonRegisteredKeyPress(IntPtr wParam, int vkCode, KeyPr return false; } - - private static void SendLWinKeyDown() { _isSimulatingKeyPress = true;