Search code examples
autohotkey

Adding action on button press (v1)


How do I make an specific function happen if an specific button is pressed?

For example, there is a variable named ef. If I press Ctrl+Alt+;, a MsgBox will pop up, with the buttons Abort, Retry and Ignore. So, if I press Abort, the variable ef‘s value will become "efabort". If I press Retry, its value will become "efretry". Otherwise, if I press Ignore, ef‘s value will become "efignore". Is that possible? If yes, please leave an answer containing the code.

Start with this:

ef = 000

<!<^;::
MsgBox,2,set ef,what do you want to set ef to?

Any answers? Thanks.

Edit:- I know, there are a lot of things that went wrong while making this post. I did not use <kbd> </kbd>, did not use proper capitalization, grammar, yada yada. Plus, I did not even take the time to sift through the offline docs, let alone the online ones! So, no research effort was shown either. Really sorry.

Plus, this question is from the v1 era. So, no use of taking information from it.


Solution

  • A variable is a placeholder for a value. The value can change, nevertheless a variable can only hold one value at a time.

    To store a value in a variable is recommended to use the expression method that uses the colon-equal operator (:=).

    ef := "" ; initialize the variable in the auto-execute section (top of the script) 
    
    MsgBox,2,set ef,what do you want to set ef to?
    
    IfMsgBox Abort
        ef := "efabort"
    IfMsgBox Retry
        ef := "efretry"
    IfMsgBox Ignore
        ef := "efignore"
    
    ; To return a given value of a variable within commands, you need to enclose the variable in percent signs
    
    MsgBox % "The value in the variable ef is now " . ef . "."