Increment variable amount by 1 with each press of button

Get help with using AutoHotkey (v2 or newer) and its commands and hotkeys
badkarma5150
Posts: 3
Joined: 04 Sep 2017, 13:38

Increment variable amount by 1 with each press of button

Post by badkarma5150 » 25 Mar 2023, 12:18

Apologies for the simple question but have pounded my head against the wall trying to figure this very simple action out. Very new user here. I want to be able to increase the value of a variable each time I press a specific key. That's it.. in SQL I would reference the variable itself and simply add 1 (eg. Var = Var +1) but can't do that here it seems so how do I do this?

The following code will add 1 to the variable but won't keep adding higher from there... just keeps saying 1 which means the var gets reset each time I press the assigned key so something basic is eluding me.

Code: Select all

^j::
{

FileNumber := 0
FileNumber := ++FileNumber

MsgBox(FileNumber)


}

User avatar
mikeyww
Posts: 26885
Joined: 09 Sep 2014, 18:38

Re: Increment variable amount by 1 with each press of button

Post by mikeyww » 25 Mar 2023, 12:27

Welcome to this AutoHotkey forum!

What is eluding you? When you trigger a hotkey or other function, each statement in the function will execute (or according to flow rules & certain operators). Follow the script line by line to understand what it does.

Instead of resetting the value each time, just set it once. :arrow: Static will do that.

Code: Select all

#Requires AutoHotkey v2.0
^j:: {
 Static FileNumber := 0
 FileNumber++
 MsgBox FileNumber
}

badkarma5150
Posts: 3
Joined: 04 Sep 2017, 13:38

Re: Increment variable amount by 1 with each press of button

Post by badkarma5150 » 25 Mar 2023, 12:40

Awesome! Thank you so much! :)

Post Reply

Return to “Ask for Help (v2)”