Mario R
Mario R

Reputation: 13

Autohot key variable function not global

I'm trying to make a function that takes coordinates, copies that field and name a variable, but the variable seems to be local and doesn't transfer out of the function. The coordinates (x,y) and delay seem to be working fine, but the desc variable always comes out blank. Please help!

newest(x, y, delay, variable)

   {


 sleep, %delay%



    click, %x%,%y%  ;desc machine field
    clipboard =           ; empty the clipboard 
    Loop
    {
        If GetKeyState("F2","P")  ; terminate the loop whenever you want by pressing F2
        break
        Send {Ctrl Down}c{Ctrl Up}
            if  clipboard is not space   
                break  ; Terminate the loop

        }
    variable := clipboard ;
    msgbox %variable%
    return 

   }

^k::

newest(654, 199, 200, desc)

msgbox %desc%

return

Upvotes: 1

Views: 110

Answers (1)

Relax
Relax

Reputation: 10543

From the function's point of view, parameters are essentially the same as local variables unless they are defined as ByRef.

ByRef makes one parameter of the function an alias for a variable, allowing the function to assign a new value to this variable.

newest(x, y, delay, ByRef variable){
    sleep, %delay%
    click, %x%,%y%   ; desc machine field
    clipboard =           ; empty the clipboard 
    Loop
    {
        If GetKeyState("F2","P")  ; terminate the loop whenever you want by pressing F2
            break
        Send {Ctrl Down}c{Ctrl Up}
        If  clipboard is not space   
            break  ; Terminate the loop
    }
    variable := clipboard
    msgbox %variable%
    return 
}

^k::
    newest(54, 199, 200, desc)
    msgbox %desc%
return

Upvotes: 1

Related Questions