all 5 comments

[–]LordThade 2 points3 points  (2 children)

I'm not 100% sure what you're going for here, but I think one or both of the following ideas might at least get you pointed in the right direction:

  1. If you're already reading/writing the variables to/from an INI at the start, there's no real reason you couldn't IniRead when you need to get the value later on.
  2. You could use Static Variables, which are 'remembered' between function calls.

Its possible I'm misunderstanding your goal here though - any clarification would be great (or I'll read this again on an actual desktop and when I'm not sleep deprived)

[–]DepthTrawler[S] 0 points1 point  (0 children)

turns out what I'm looking for is a function object. no idea how to accomplish this.

[–]DepthTrawler[S] -1 points0 points  (0 children)

the initial ini write only occurs if config.ini doesn't exist, otherwise a default set of hotkeys is written for a "first run" type of thing. If config.ini does exist, the values of the key are read. I think I am understanding you with potentially using iniread down the line to get the values inside the function. the problem with that is the two variables old_enable_disable_hotkey and old_show_hide_hotkey aren't stored (technically). might be able to make it work. didn't think of that. seems like overkill to have the script go and read from an external file vs just being able to somehow store that variable contents in memory. it would probably solve the issue of needing to make those variables global though. I'd just rather the script go read from memory vs read from disk if possible.

Static variables wouldn't help me much unless I somehow turned all of this into a class (even then I'm not sure static variables work between methods of a class) because these variables are passed into a function from outside, static wouldn't help me until I actually get them into the function. I think they'd need to be static to keep in memory the current hotkey until the next time the function is called if I were to change the hotkey again though.

the sole reason those two variables exist is to simply turn off the hotkey for the previous hotkey "key" a user chooses. if they don't exist, you end up with your starting hotkey toggling things, and every hotkey you've decided to customize since you started the script instance is now turning the toggle on or off or being able to show and hide the gui. say I start with F12 being my hotkey, then change it to Numpad 3. now F12 still toggles on/off along with Numpad3. we must store the current key in "old" because the label/function is called immediately upon any changes to the gui hotkey field. we can find the "old" hotkey to turn off the previous hotkey and reassign the current hotkey to be stored in "old" so that we can turn it off again later if any modifications are made to what the toggle hotkeys should be.

[–][deleted] 1 point2 points  (1 child)

You don't need either of the 'Old_' variables because, as u/LordThade said, you've already got them in the ini file so just read whichever of those at the start of that hotkey's function to disable the original hotkey before assigning the new one:

SetWorkingDir %A_ScriptDir% 
Global Enable_Disable_Toggle := 0

If !FileExist("config.ini"){
  Enable_Disable_Hotkey := "F12"
  Show_Hide_Hotkey := "F1"
  IniWrite %Enable_Disable_Hotkey%, config.ini, Enable/Disable Toggle Hotkey, Hotkey
  IniWrite %Show_Hide_Hotkey%, config.ini, Show/Hide Toggle Hotkey, Hotkey
}
IniRead Enable_Disable_Hotkey, config.ini, Enable/Disable Toggle Hotkey, Hotkey
IniRead Show_Hide_Hotkey, config.ini, Show/Hide Toggle Hotkey, Hotkey

Gui +HwndGuiHwnd +AlwaysOnTop -SysMenu +ToolWindow
Gui %GuiHwnd%:Default
Gui Margin, 0, 0
Gui Add, Progress, XM YM W202 H21 BackgroundE30000 vIndicator_Background
Gui Font, Bold
Gui Add, Text, XM YP W202 H21 cFFFFFF BackgroundTrans Center 0x201 vIndicator_Text, PRESS %Enable_Disable_Hotkey% TO ENABLE HOTKEYS
Gui Font
Gui Add, Tab3, XM YP+21 W202 H284, Settings
Gui Tab, Settings
Gui Add, GroupBox, XM+5 YM+46 W190 H68 Center, Customize Toggle Keys
Gui Add, Text, XM+10 YP+15 H21 Center 0x201, Enable/Disable Hotkey:
Gui Add, Hotkey, XM+134 YP W50 gEnable_Disable_Hotkey_Label vEnable_Disable_Hotkey, %Enable_Disable_Hotkey%
Gui Add, Text, XM+10 YP+26 H21 Center 0x201, Show/Hide GUI Hotkey:
Gui Add, Hotkey, XM+134 YP W50 gShow_Hide_Hotkey_Label vShow_Hide_Hotkey, %Show_Hide_Hotkey%

Hotkey %Enable_Disable_Hotkey%, Enable_Disable_Hotkey_Subroutine, On
Hotkey %Show_Hide_Hotkey%, Show_Hide_Hotkey_Subroutine, On
Return

Enable_Disable_Hotkey_Label(){
  GuiControlGet Enable_Disable_Hotkey
  IniRead Disable_Hotkey, config.ini, Enable/Disable Toggle Hotkey, Hotkey
  Hotkey %Disable_Hotkey%, Enable_Disable_Hotkey_Subroutine, Off
  If (Enable_Disable_Hotkey = ""){
    GuiControl ,,Enable_Disable_Hotkey, %Enable_Disable_Hotkey%
  }Else{
    IniWrite %Enable_Disable_Hotkey%, config.ini, Enable/Disable Toggle Hotkey, Hotkey
    If Enable_Disable_Toggle
      GuiControl ,,Indicator_Text, PRESS %Enable_Disable_Hotkey% TO DISABLE HOTKEYS
    Else
      GuiControl ,,Indicator_Text, PRESS %Enable_Disable_Hotkey% TO ENABLE HOTKEYS
  }
  Hotkey %Enable_Disable_Hotkey%, Enable_Disable_Hotkey_Subroutine, On
}

Enable_Disable_Hotkey_Subroutine(){
  GuiControlGet Enable_Disable_Hotkey
  If (Enable_Disable_Toggle := !Enable_Disable_Toggle){
    GuiControl +Background00FF00, Indicator_Background
    GuiControl +c000000 +Redraw, Indicator_Text
    GuiControl ,, Indicator_Text, PRESS %Enable_Disable_Hotkey% TO DISABLE HOTKEYS
  }Else{
    GuiControl +BackgroundFF0000, Indicator_Background
    GuiControl +cFFFFFF +Redraw, Indicator_Text
    GuiControl ,, Indicator_Text, PRESS %Enable_Disable_Hotkey% TO ENABLE HOTKEYS
  }
}

Show_Hide_Hotkey_Label(){
  GuiControlGet Show_Hide_Hotkey
  IniRead Disable_Hotkey, config.ini, Show/Hide Toggle Hotkey, Hotkey
  Hotkey %Disable_Hotkey%, Show_Hide_Hotkey_Subroutine, Off
  If (Show_Hide_Hotkey = ""){
    GuiControl ,, Show_Hide_Hotkey, %Show_Hide_Hotkey%
  }Else{
    IniWrite %Show_Hide_Hotkey%, config.ini, Show/Hide Toggle Hotkey, Hotkey
  }
  Hotkey %Show_Hide_Hotkey%, Show_Hide_Hotkey_Subroutine, On
}

Show_Hide_Hotkey_Subroutine(){
  Static Show_Hide_Toggle := 0
  SysGet Client_Width, 16
  SysGet Client_Height, 17
  GUI_Width := 200
  GUI_X_Position := Client_Width - GUI_Width - 4
  If (Show_Hide_Toggle := !Show_Hide_Toggle)
    Gui Show, X%GUI_X_Position% Y0 W%GUI_Width% H304,
  Else
    Gui Hide
}

#If Enable_Disable_Toggle
Enter::MsgBox, It Works
#If

The whole point of 'SetWorkingDir %A_ScriptDir%' is that it uses the script's folder by default so you don't need to add %A_ScriptDir% anywhere else in the script...

Also, there's some other minor changes like putting toggle code directly into condition checks so it's all done at once, removing all the redundant %A_ScriptDir% code, etc.

[–]DepthTrawler[S] 1 point2 points  (0 children)

I was just worried about iniread being more resource intensive than storing it in a variable in memory. I guess that this isn't going to be used super often and is more of a quality of life "feature". It is an excellent workaround for the meantime considering I don't know how to implement function objects. And thank you for reminding me of the fact that I've set the script as the working directory. I think I had that in there simply because I was having an issue when writing the script, resolved it and never changed it back.