From 23ad7f1940d4e7930f72df01876de8cc96a246ce Mon Sep 17 00:00:00 2001 From: Mario Alegre Date: Sun, 15 Nov 2020 18:21:40 -0500 Subject: [PATCH] reorganized library files --- lib/{General.ahk => HoldRepeat.ahk} | 115 ++++++++++++++-------------- lib/Prelude.ahk | 22 ++++++ src/Minecraft.ahk | 23 ++---- 3 files changed, 83 insertions(+), 77 deletions(-) rename lib/{General.ahk => HoldRepeat.ahk} (55%) create mode 100644 lib/Prelude.ahk diff --git a/lib/General.ahk b/lib/HoldRepeat.ahk similarity index 55% rename from lib/General.ahk rename to lib/HoldRepeat.ahk index 680eb11..3a56f1d 100644 --- a/lib/General.ahk +++ b/lib/HoldRepeat.ahk @@ -1,60 +1,57 @@ -; Set default location and name convention for compiled scripts to use -;@Ahk2Exe-ExeName %A_MyDocuments%\AutoHotkey\bin\AHK-%A_ScriptName% - -; Install hooks to make sure script can get physical state of keys instead of logical state. -#InstallKeybdHook -#InstallMouseHook - -; Settings -#NoEnv ; Recommended for performance and compatibility with future AutoHotkey releases. -SendMode Input ; Recommended for new scripts due to its superior speed and reliability. -SetWorkingDir %A_ScriptDir% ; Ensures a consistent starting directory. -#UseHook ; All hotkeys are implemented through keyboard hook to avoid script activating itself. -#MenuMaskKey vkFF - -; -; Function definitions -; - -Hold(Primary, Secondary) { - Send {%Primary% down} - if ( Secondary.Length() != 0 ) { - ThisLoop := Func("HoldLoop").bind(Primary,Secondary) - SetTimer, %ThisLoop%, 50 - } -} - -HoldLoop(Primary, Secondary) { - if GetKeyState(Primary,"P") { - SetTimer, , Delete - } - for index,key in Secondary { - if GetKeyState(key,"P") { - Send {%Primary% up} - SetTimer, , Delete - } - } -} - -Repeat(Primary, Secondary) { - Send {%Primary% up} - Sleep 100 - RepeatVar := 0 - Loop { - if GetKeyState(Primary,"P") { - return - } - for index,key in Secondary { - if GetKeyState(key,"P") { - return - } - } - if (RepeatVar == 2) { - SendEvent, {%Primary%} - RepeatVar := 1 - } else { - RepeatVar++ - } - Sleep 50 - } +; +; Functions for holding and repeating keys +; + +Hold(Primary, Secondary) { + Send {%Primary% down} + if ( Secondary.Length() != 0 ) { + ThisLoop := Func("HoldLoop").bind(Primary,Secondary) + SetTimer, %ThisLoop%, 50 + } +} + +HoldLoop(Primary, Secondary) { + if GetKeyState(Primary,"P") { + SetTimer, , Delete + } + for index,key in Secondary { + if GetKeyState(key,"P") { + Send {%Primary% up} + SetTimer, , Delete + } + } +} + +Repeat(Primary, Secondary) { + Send {%Primary% up} + Sleep 100 + RepeatVar := 0 + Loop { + if GetKeyState(Primary,"P") { + return + } + for index,key in Secondary { + if GetKeyState(key,"P") { + return + } + } + if (RepeatVar == 2) { + SendEvent, {%Primary%} + RepeatVar := 1 + } else { + RepeatVar++ + } + Sleep 50 + } +} + +; Sets key to hold if single tap, or repeat if double tap +HoldOrRepeat(Primary, Secondary) { + Send {%Primary% down} + KeyWait, %Primary%, D, T0.2 + if (ErrorLevel = 0) { + Repeat(Primary,Secondary) + } else { + Hold(Primary,Secondary) + } } \ No newline at end of file diff --git a/lib/Prelude.ahk b/lib/Prelude.ahk new file mode 100644 index 0000000..952137c --- /dev/null +++ b/lib/Prelude.ahk @@ -0,0 +1,22 @@ +; +; General Settings +; + +; Set default location and name convention for compiled scripts to use +;@Ahk2Exe-ExeName %A_MyDocuments%\AutoHotkey\bin\AHK-%A_ScriptName% + +; Install hooks to make sure script can get physical state of keys instead of logical state. +#InstallKeybdHook +#InstallMouseHook + +#NoEnv ; Recommended for performance and compatibility with future AutoHotkey releases. + +SendMode Input ; Recommended for new scripts due to its superior speed and reliability. + +SetWorkingDir %A_ScriptDir% ; Ensures a consistent starting directory. + +; All hotkeys are implemented through keyboard hook to avoid script activating itself. +#UseHook + +; MenuMaskKey is set to a virtual key that is defined by Windows as doing nothing +#MenuMaskKey vkFF \ No newline at end of file diff --git a/src/Minecraft.ahk b/src/Minecraft.ahk index 1762fdc..ac4d504 100644 --- a/src/Minecraft.ahk +++ b/src/Minecraft.ahk @@ -5,8 +5,9 @@ ; Front Matter ; -; Include general library -#Include ../lib/General.ahk +; Include relevant libraries +#Include ../lib/Prelude.ahk +#Include ../lib/HoldRepeat.ahk ; Only do hotkeys if relevant window is active #If WinActive("Minecraft") @@ -20,22 +21,8 @@ ; Hold/Autoclick mouse buttons ; Holds if single click, autoclicks if double click -Tab & LButton UP:: -KeyWait, LButton, D, T0.2 -if (ErrorLevel = 0) { - Repeat("LButton",["RButton","MButton"]) -} else { - Hold("LButton",["RButton","MButton"]) -} -return -Tab & RButton UP:: -KeyWait, RButton, D, T0.2 -if (ErrorLevel = 0) { - Repeat("RButton",["LButton","MButton"]) -} else { - Hold("RButton",["LButton","MButton"]) -} -return +Tab & LButton UP::HoldOrRepeat("LButton",["RButton","MButton"]) +Tab & RButton UP::HoldOrRepeat("RButton",["LButton","MButton"]) ; Autorun Tab & w UP::Hold("w",["s"])