Holding variable state between hotkey occurences Topic is solved

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
Dranh

Holding variable state between hotkey occurences  Topic is solved

21 Sep 2018, 01:29

I have a question surrounding how to make a variable initialize once and hold its state between invokes\occurrences of a hotkey being triggered. In my example below i have declared 'vMyVar' equal to A_TickCount and I understand this happens everytime F4 is invoked given this approach. Is there a way to have this occur only once and the variable hold its state between invokes of F4? Any pointers to examples/concepts i've been unable to find for myself would be much appreciated.

Code: Select all

F4::
vMyVar := A_TickCount
KeyWait, F4, D;
/* do stuff and calculations with vMyVar */
MsgBox Value of %vMyVar%
User avatar
TheDewd
Posts: 1510
Joined: 19 Dec 2013, 11:16
Location: USA

Re: Holding variable state between hotkey occurences

21 Sep 2018, 08:12

Not sure what you goal is, but here's one way:

Code: Select all

#SingleInstance, Force

vMyVar := 0 ; Initialize variable, starting with value of 0

F4:: ; F4 Hotkey
	; If "vMyVar" is 0, get A_TickCount.
	; If not 0, it must already have A_TickCount from previous execution. Show existing value instead of getting new value.
	vMyVar := (vMyVar ? vMyVar : A_TickCount)
	
	; Display value in MessageBox
	MsgBox, % vMyVar
return

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: No registered users and 291 guests