From 59b8ab5e57984a076788ccd0f226c15fc7c7b6e9 Mon Sep 17 00:00:00 2001 From: PhatPhuckDave Date: Wed, 1 Jan 2025 00:49:44 +0100 Subject: [PATCH] Refactor more shit to be more sensible --- Echo.ahk2 | 43 ++++++++++++++++++++++++++++--------------- 1 file changed, 28 insertions(+), 15 deletions(-) diff --git a/Echo.ahk2 b/Echo.ahk2 index 559b32d..bc4863e 100644 --- a/Echo.ahk2 +++ b/Echo.ahk2 @@ -12,7 +12,7 @@ ih.Start() addNext := 0 removeNext := 0 -enabledWindows := [[], []] +enabledWindows := Map() keyLog := Map() whitelistedKeys := Map() paused := 0 @@ -24,7 +24,9 @@ OnKeyDown(InputHook, VK, SC) { if (paused) { return } - key := GetKeyName("%vk" . VK . "sc" . SC "%") + hexVK := Format("{:X}", VK) + hexSC := Format("{:X}", SC) + key := GetKeyName("vk" . hexVK . "sc" . hexSC) if (addNext) { addNext := 0 if (whitelistedKeys.Has(key)) { @@ -51,7 +53,7 @@ OnKeyDown(InputHook, VK, SC) { } if (whitelistedKeys.Has(key)) { for k, v in enabledWindows[1] { - ControlSend("", "{" . key . " down}", "ahk_id " . v) + ControlSend("{" . key . " down}", "ahk_id " . v) } } if (!keyLog.Has(key)) { @@ -67,7 +69,7 @@ OnKeyUp(InputHook, VK, SC) { key := GetKeyName("%vk" . VK . "sc" . SC "%") if (whitelistedKeys.Has(key)) { for k, v in enabledWindows[1] { - ControlSend("", "{" . key . " up}", "ahk_id " . v) + ControlSend("{" . key . " up}", "ahk_id " . v) } } } @@ -183,26 +185,33 @@ WhitelistKeys(string) { } AddCurrentWindow() { + global enabledWindows activeID := WinGetID("A") - if (Find(enabledWindows[1], activeID) = 0) { - activeName := WinGetProcessName("A") - enabledWindows[1].Push(activeID) - enabledWindows[2].Push(activeName) - ToolTip("Added " . activeID . " (" . activeName . ") to echo list") + if (enabledWindows.Has(activeID)) { + ToolTip("Window already in echo list") SetTimer(ToolTip, -500) + return } + activeName := WinGetProcessName("A") + enabledWindows.Set(activeID, activeName) + ToolTip("Added " . activeID . " (" . activeName . ") to echo list") + SetTimer(ToolTip, -500) + return } RemoveCurrentWindow() { + global enabledWindows activeID := WinGetID("A") - index := Find(enabledWindows[1], activeID) - if (index != 0) { - activeName := enabledWindows[2][index] - enabledWindows[1].Remove(index) - enabledWindows[2].Remove(index) - ToolTip("Removed " . activeID . " (" . activeName . ") from echo list") + if (!enabledWindows.Has(activeID)) { + ToolTip("Window not found in echo list") SetTimer(ToolTip, -500) + return } + activeName := enabledWindows[activeID] + enabledWindows.Delete(activeID) + ToolTip("Removed " . activeID . " (" . activeName . ") from echo list") + SetTimer(ToolTip, -500) + return } ResetHook() { @@ -217,6 +226,7 @@ ResetHook() { } TogglePause() { + global paused if (paused) { ToolTip("Unpaused") SetTimer(ToolTip, -500) @@ -230,6 +240,7 @@ TogglePause() { } F5:: { + global ih Reload ; ih.Stop() ; ih := InputHook("B") @@ -240,12 +251,14 @@ F5:: { } F3:: { + global addNext ToolTip("Adding key") SetTimer(ToolTip, -500) addNext := 1 } F4:: { + global removeNext ToolTip("Removing key") SetTimer(ToolTip, -500) removeNext := 1