automationautoit

Bind content with range to an key


I want to bind some variable textcontent to an specific key (example on key p)

For example:

First press on p: "hannes1"

second press on p: "hannes2"

third press on p: "hannes3"

And so on. Thanks for any help.


Solution

  • It isn't clear from your question if you want a fixed string hannes with a counter or "the next string from a list", so I added both options (available via p and P

    ; Global $aVars[3] = ["hannes1", "hannes2", "hannes3"]
    HotKeySet("p", "_SendVarArray")
    HotKeySet("P", "_sendvarCount")
    
    While True
        Sleep(25)
    WEnd
    
    Func _SendVarArray() ; write strings from an array; rotating
        Local $aVars[3] = ["One", "Two", "Three"] ; define EITHER here as Local OR in main program as Global
        Local Static $x = UBound($aVars) - 1 ; initial value for next line to be changed as start value '0'
        $x = Mod($x + 1, UBound($aVars))
        Send($aVars[$x])
    EndFunc   ;==>_SendVarArray
    
    Func _SendVarCount()
        Local Static $x = 0 ;  'Static' to remember value with next execution
        $x += 1
        Send("hannes" & $x)
    EndFunc   ;==>_SendVarCount
    

    NOTE: an ordinary letter is a bad choice for a hotkey, because if a string you send() contains that letter, it will activate the hotkey function too (I kept it anyway, leaving it to you to find a better key(combination)).

    The advantage of HotKeySet() is that your program can do other things while waiting for the hotkey being pressed.