[SOLVED] HOTKEY PRESS COUNTER - saving variable value to ini & zero error Topic is solved

Get help with using AutoHotkey and its commands and hotkeys
jv6642
Posts: 14
Joined: 16 Apr 2019, 02:37

[SOLVED] HOTKEY PRESS COUNTER - saving variable value to ini & zero error

17 Apr 2019, 06:00

I have a two part question

question 1: I am trying to add Var1 + Var2, although when it is (ex 3+0) , nothing is showing. the only time the addition works is when there is a number other than zero for Var1 & Var2, how do I get it to work with zero?

question 2: how would I save Var1 , Var2 ,Var3 , Var4 etc to ini file or clipboard? every time I reload variables disappear.


Var1 = 0
ScrollLock::
Var1++
return

Var2 = 0
!ScrollLock::
Var2++
return


Var5 = 0
!d::
Var5 := (Var1 + Var2)
MsgBox,,title,Text
(

1. Option 1 = %Var5%

)
return

`::
Reload
return
Last edited by jv6642 on 18 Apr 2019, 08:46, edited 5 times in total.
scriptors
Posts: 178
Joined: 25 Feb 2016, 09:01

Re: saving variable value to ini

17 Apr 2019, 06:16

just little portion of my script, try and modify

xx1 - yy1 - color1 are variable
xx - yy - color are section (but now i not remember exactly)

Code: Select all

	IniWrite, %xx%, C:\Users\AP42\Desktop\Eroi.ini, %title%, xx1
	IniWrite, %yy%, C:\Users\AP42\Desktop\Eroi.ini, %title%, yy1
	IniWrite, %color%, C:\Users\AP42\Desktop\Eroi.ini, %title%, color1
next .. how to read variable

Code: Select all

IniRead, ApoX, C:\Users\AP42\Desktop\Eroi.ini, %title%, xx1
IniRead, ApoY, C:\Users\AP42\Desktop\Eroi.ini, %title%, yy1
IniRead, Colo1, C:\Users\AP42\Desktop\Eroi.ini, %title%, color1
IniRead, Colo2, C:\Users\AP42\Desktop\Eroi.ini, %title%, color1
IniRead, Colo3, C:\Users\AP42\Desktop\Eroi.ini, %title%, color1
IniRead, Colo4, C:\Users\AP42\Desktop\Eroi.ini, %title%, color1
see Help for IniWrite and IniRead
jv6642
Posts: 14
Joined: 16 Apr 2019, 02:37

Re: saving variable value to ini

17 Apr 2019, 06:57

well I tried to put in my code but its not working, im new at this and im not even sure where to place it.
Albireo
Posts: 752
Joined: 16 Oct 2013, 13:53

Re: saving variable value to ini

17 Apr 2019, 11:10

Code: Select all

A = 0
B = 3
C := A+B
MsgBox A = %A% `nB = %B% `n`nC = %C%

; Save to ClipBoard
ClipBoard = %C%
Don't know if there was anything more you asked for?
jv6642
Posts: 14
Joined: 16 Apr 2019, 02:37

Re: saving variable value to ini

17 Apr 2019, 16:14

That seems to be what i have but its nnot working.

Var1++ is being counted by ahk, and the result gets put in var1. This is counting how many time i press the hotkey. I do not actually enter a number

Var5 := (Var1 + Var2)

So, When var1 or var2 is a 0 nothing is showing in the msgbox. Or say i have just pressed a hotkey 6 times into var1 , var5 should show 6. Its like the formula is making an error if a zero or nothing is in it.

Formulas how i think they should work

Example
6 hotkey button presses =6
Var5=var1+var2
6=6+0
6=0+6
0=0+0 (this is basically what msgbox should Show when I haven’t pressed any hotkeys)


The second question was how to save the var1 to clipboard and then recall on reload.
Albireo
Posts: 752
Joined: 16 Oct 2013, 13:53

Re: saving variable value to ini

17 Apr 2019, 17:09

Show your program code!
jv6642
Posts: 14
Joined: 16 Apr 2019, 02:37

Re: saving variable value to ini

17 Apr 2019, 18:42

This is my code and it works, but if you press ScrollLock only msgbox is blank. only becomes visable when !ScrollLock is also pressed.

Var1 = 0
ScrollLock::
Var1++
return

Var2 = 0
!ScrollLock::
Var2++
return


Var5 = 0
!d::
Var5 := (Var1 + Var2)
MsgBox,,title,Text
(

1. Option 1 = %Var5%

)
return

`::
Reload
return
Last edited by jv6642 on 17 Apr 2019, 18:54, edited 1 time in total.
jv6642
Posts: 14
Joined: 16 Apr 2019, 02:37

Re: saving variable value to ini

17 Apr 2019, 18:48

I want something like this:


Var1 = 0
ScrollLock::
Var1++
return

Var2 = 0
!ScrollLock::
Var2++
return


Var5 = 0
!d::
Var5 := (Var1 + Var2)

;possibly some IF statement here saying if 0 show zero

MsgBox,,title,Text
(

1. Option 1 = %Var5%

)
return

`::
ClipBoard = %Var1% ; saves Var1 to clipboard
ClipBoard = %Var2% ; saves Var2 to clipboard
Reload
%Var1%=ClipBoard ; calls Var1 from clipboard
%Var2%=ClipBoard ; calls Var2 from clipboard
return
Albireo
Posts: 752
Joined: 16 Oct 2013, 13:53

Re: saving variable value to ini

17 Apr 2019, 19:10

Maybe this?

Code: Select all

Var1 = 0
Var2 = 0
Var5 = 0

ScrollLock::
	Var1++
Return 

!ScrollLock::
	Var2++
Return 

!d::
	Var5 := (Var1 + Var2)
	;possibly some IF statement here saying if 0 show zero

	MsgBox,,title,
	(	Text
		1. Option 1 = %Var5%
	)
Return

`::
	ClipBoard = %Var1% ; saves Var1 to clipboard
	ClipBoard = %Var2% ; saves Var2 to clipboard
	Reload
	%Var1%=ClipBoard ; calls Var1 from clipboard
	%Var2%=ClipBoard ; calls Var2 from clipboard
Return
This feels not correct
ClipBoard = %Var1% ; saves Var1 to clipboard
ClipBoard = %Var2% ; saves Var2 to clipboard
You don't need the first line!
jv6642
Posts: 14
Joined: 16 Apr 2019, 02:37

Re: saving variable value to ini

17 Apr 2019, 19:31

Thank you the zero issue is fixed , but the saving Var1 and Var2 to clipboard isn't working, after reload Var1 and Var2 go back to zero
jv6642
Posts: 14
Joined: 16 Apr 2019, 02:37

Re: saving variable value to ini

17 Apr 2019, 22:25

any help or ideas on how to save these variables (Var1, Var2, Var5) to clip board would be appreciated. values disappear on reload


Var1 = 0
Var2 = 0
Var5 = 0

ScrollLock::
Var1++
Return

!ScrollLock::
Var2++
Return

!d::
Var5 := (Var1 + Var2)
MsgBox,,title,
( Text
1. Option 1 = %Var5%
)
Return

`::
ClipBoard = %Var1% ; saves Var1 to clipboard
ClipBoard = %Var2% ; saves Var2 to clipboard
Reload
%Var1%=ClipBoard ; calls Var1 from clipboard
%Var2%=ClipBoard ; calls Var2 from clipboard
Return
Albireo
Posts: 752
Joined: 16 Oct 2013, 13:53

Re: saving variable value to clipboard & zero error

18 Apr 2019, 04:44

Maybe something like this?
- Clipboard can only save one value at a time. (you must use an ini-file to save more)
- Instructions after Reload are never executed. (in your program)
- When this program is started, all variables that have no value have the default value "0" (see IniRead)
- I have added alt+c (reset the inifile), Alternatively, the ini-file can be deleted - it is re-created when this program is started.
- I have added ESC (this program ends)
- In several places the ini-file is opened, to view the content
- On the MsgBox you can see where in the program you are, and the result.
(The program is not fully tested)

Code: Select all

; Initiate the program ===========================================
#SingleInstance force

IniFileName = %A_ScriptDir%\myInifile.ini
VarNo = 2

; Create the ini file structure if the file does not exist
If !FileExist(IniFileName)	; Create the ini file structure if the file does not exist
{	Loop % VarNo
	{	IniWrite 0, %IniFileName%, Saved Variables, Var%A_Index%
	}
	
	IniWrite 0, %IniFileName%, Result Variables, Var5
	; Run Notepad %IniFileName%	; Show the ini file
}

; Read the values from the inifile
IniRead Var1, %IniFileName%, Saved Variables, Var1, 0
IniRead Var2, %IniFileName%, Saved Variables, Var2, 0
IniRead Var5, %IniFileName%, Result Variables, Var5, 0
Run Notepad %IniFileName%	; Show the ini file
MsgBox ,, Row %A_LineNumber% -> %A_ScriptName%, In the beginning! `n`nVar1 = %Var1% `nVar2 = %Var2% `nVar5 = %Var5%


; Hotkeys ==========================================================
ScrollLock::
	Var1++
Return 

!ScrollLock::
	Var2++
Return 

!d::
	Var5 := (Var1 + Var2)
	;possibly some IF statement here saying if 0 show zero

	MsgBox 64 , Row %A_LineNumber% -> %A_ScriptName%, Title,
	(	Text
		1. Option 1 = %Var5%
	)
Return

; Clear the ini-file
!c::
	IniWrite 0, %IniFileName%, Saved Variables, Var1		; ClipBoard = %Var1% ; saves Var1 to clipboard
	IniWrite 0, %IniFileName%, Saved Variables, Var2		; ClipBoard = %Var2% ; saves Var2 to clipboard
	IniWrite 0, %IniFileName%, Result Variables, Var5
	Run Notepad %IniFileName%	; Show the ini file
Return

; Save the result in the inifile and restart the program
`::
	IniWrite %Var1%, %IniFileName%, Saved Variables, Var1		; ClipBoard = %Var1% ; saves Var1 to clipboard
	IniWrite %Var2%, %IniFileName%, Saved Variables, Var2		; ClipBoard = %Var2% ; saves Var2 to clipboard
	IniWrite %Var5%, %IniFileName%, Result Variables, Var5
	Reload
Return

ESC::
	MsgBox 16 , Row %A_LineNumber% -> %A_ScriptName%, ESC is pressed! This program will end!
	Run Notepad %IniFileName%	; Show the ini file
	ExitApp
Return
jv6642
Posts: 14
Joined: 16 Apr 2019, 02:37

Re: saving variable value to clipboard & zero error

18 Apr 2019, 05:12

looks like it functioning, I will adjust a little, thank you
Albireo
Posts: 752
Joined: 16 Oct 2013, 13:53

Re: saving variable value to ini & zero error

18 Apr 2019, 08:11

you are welcome! (mark this question as solved - if it is!)
jv6642
Posts: 14
Joined: 16 Apr 2019, 02:37

Re: saving variable value to ini & zero error  Topic is solved

18 Apr 2019, 08:44

This is the final code - Hotkey Press Counter using INI - - Again huge thanks to Albireo

; Initiate the program ===========================================
#SingleInstance force

IniFileName = %A_ScriptDir%\myInifile.ini
VarNo = 2

If !FileExist(IniFileName) ; Create the ini file structure if the file does not exist
{ Loop % VarNo
{ IniWrite 0, %IniFileName%, Saved Variables, Var%A_Index%
}
IniWrite 0, %IniFileName%, Result Variables, Var5
}

; Read the values from the inifile
IniRead Var1, %IniFileName%, Saved Variables, Var1, 0
IniRead Var2, %IniFileName%, Saved Variables, Var2, 0
IniRead Var5, %IniFileName%, Result Variables, Var5, 0

; Hotkeys =========================================================
ScrollLock::
Var1++
Return

!ScrollLock::
Var2++
Return

!d::
Var5 := (Var1 + Var2)
MsgBox,, Title,
( Text
1. Option 1 = %Var5%
)
Return

; Clear the ini-file
!c::
IniWrite 0, %IniFileName%, Saved Variables, Var1
IniWrite 0, %IniFileName%, Saved Variables, Var2
IniWrite 0, %IniFileName%, Result Variables, Var5
IniRead Var1, %IniFileName%, Saved Variables, Var1, 0
IniRead Var2, %IniFileName%, Saved Variables, Var2, 0
IniRead Var5, %IniFileName%, Result Variables, Var5, 0
;Run Notepad %IniFileName% ; Show the ini file
Return

; Save the result in the inifile and restart the program
`::
IniWrite %Var1%, %IniFileName%, Saved Variables, Var1
IniWrite %Var2%, %IniFileName%, Saved Variables, Var2
IniWrite %Var5%, %IniFileName%, Result Variables, Var5
Reload
Return

ESC::
MsgBox 16 ,HotKey Press Counter, ESC is pressed! This program will end!
;Run Notepad %IniFileName% ; Show the ini file
ExitApp
Return

Return to “Ask For Help”

Who is online

Users browsing this forum: Albireo, Bing [Bot], Flipeador, gummby8, jp94189, kostons, Odlanir, teadrinker, vsub and 182 guests