asidi Posted November 7, 2019 Share Posted November 7, 2019 Hi all, I created a script that is supposed to change the value of a command (enabledev) in an .ini file between 0 and 1 by pressing F10, but currently, it only changes the value from 1 to 0, but not the other way around. What did I do wrong here? Here is the script: expandcollapse popup#cs ---------------------------------------------------------------------------- AutoIt Version: 3.3.14.5 Author: myName Script Function: Template AutoIt script. #requireadmin ; -------------------------------------------------- ; Define variables, include files. ; -------------------------------------------------- #include <Misc.au3> Opt("TrayIconHide", 0) ;0=show, 1=hide tray icon Opt("MouseCoordMode", 0) Opt("SendKeyDelay", 0) Opt("MouseClickDelay", 0) $title = "toggle1" $file = @ScriptDir & "\config.ini" $toggle_off = 0 $toggle_on = 1 ; -------------------------------------------------- HotKeySet("{F10}", "toggleSwitch") ; -------------------------------------------------- ; Check if Script is already running ; -------------------------------------------------- If _Singleton($title, 1) = 0 Then Beep(135, 250) Exit EndIf ; -------------------------------------------------- ; Script is running - Beep! ; -------------------------------------------------- Beep(500, 250) ; ================================================== ; Main Script Loop ; ================================================== While 1 Sleep(100) WEnd Func _readToggle() $toggle_on = IniRead($file, "enabledev", "Active", 1) EndFunc ;==>_readToggle Func _writeToggleOn() IniWrite($file, "enabledev", "Active", $toggle_on) EndFunc ;==>_writeToggleOn Func _writeToggleOff() IniWrite($file, "enabledev", "Active", $toggle_off) EndFunc ;==>_writeToggleOff() Func toggleSwitch() _readToggle() If $toggle_on = 1 Then _writeToggleOff() Beep(500, 125) Else _writeToggleOn() Beep(250, 125) EndIf Sleep(100) Send("{F12}") EndFunc ;==>toggleSwitch() Link to comment Share on other sites More sharing options...
Zedna Posted November 7, 2019 Share Posted November 7, 2019 expandcollapse popup#include <Misc.au3> Opt("TrayIconHide", 0) ;0=show, 1=hide tray icon Opt("MouseCoordMode", 0) Opt("SendKeyDelay", 0) Opt("MouseClickDelay", 0) ;~ $title = "toggle1" $file = @ScriptDir & "\config.ini" Global $toggle = _readToggle() HotKeySet("{F10}", "toggleSwitch") If _Singleton($title, 1) = 0 Then Beep(135, 250) Exit EndIf Beep(500, 250) While 1 Sleep(100) WEnd Func _readToggle() Return IniRead($file, "enabledev", "Active", 1) EndFunc ;==>_readToggle Func _writeToggle() IniWrite($file, "enabledev", "Active", $toggle) EndFunc ;==>_writeToggle Func toggleSwitch() $toggle = 1 - $toggle If $toggle = 1 Then Beep(500, 125) Else Beep(250, 125) EndIf _writeToggle() Sleep(100) Send("{F12}") EndFunc ;==>toggleSwitch() asidi 1 Resources UDF ResourcesEx UDF AutoIt Forum Search Link to comment Share on other sites More sharing options...
asidi Posted November 7, 2019 Author Share Posted November 7, 2019 Thank you very much, works like a charm. But what did I do wrong exactly? I cant seem to figure out why my original script didnt change the value back from 0 to 1. Link to comment Share on other sites More sharing options...
Danp2 Posted November 7, 2019 Share Posted November 7, 2019 You were using the $toggle_on variable for two different purposes -- A constant representing the toggled on state The value read from the INI file You need two separate variables to handle this. asidi 1 Latest Webdriver UDF Release Webdriver Wiki FAQs Link to comment Share on other sites More sharing options...
Nine Posted November 7, 2019 Share Posted November 7, 2019 Constants should always be declared like this to avoid distraction errors : Global Const $toggle_off = 0 Global Const $toggle_on = 1 asidi 1 “They did not know it was impossible, so they did it” ― Mark Twain Spoiler Block all input without UAC Save/Retrieve Images to/from Text Monitor Management (VCP commands) Tool to search in text (au3) files Date Range Picker Virtual Desktop Manager Sudoku Game 2020 Overlapped Named Pipe IPC HotString 2.0 - Hot keys with string x64 Bitwise Operations Multi-keyboards HotKeySet Recursive Array Display Fast and simple WCD IPC Multiple Folders Selector Printer Manager GIF Animation (cached) Screen Scraping Multi-Threading Made Easy Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now