How do i parse a variable in a function to being able to define which variable to send? - autohotkey

This is my code
IniRead, custommessage1, whisperconfig.ini, messages, Message1
IniRead, custommessage2, whisperconfig.ini, messages, Message2
^NumPad1::whispermessage(1)
whispermessage(var){
finalwhisper := "custommessage" + var ;this equals custommessage1
Msgbox, %finalwhisper%
BlockInput On
SendInput ^{Enter}%finalwhisper%{Enter} ;<-- problem
BlockInput Off
return
}
So in the first line i am importing the value of custommessage1 (it could be "hi im henrik"). This is what i wish to end up getting as a output.
Inside the function i want the var (which is 1 in this case) to be merged with a variable called custommessage ending with a result of custommessage1
i want the endresult to do a SendInput %custommessage1%.
this way i can have one function for up to 9 triggers including var numbers.
Can anyone help? i am sure this is fairly simple however i am new to this coding thing so bear with me.

Your function only knows about its own variables, the ones you pass in as parameters, and global variables.
You're trying to access custommessage1, which is outside the function and isn't global. You either need to make all your variables global, or pass them in to the function.
I suggest the latter, using an array. Here's an example, make sure you're running the latest version of AHK for this to work properly.
IniRead, custommessage1, whisperconfig.ini, messages, Message1
IniRead, custommessage2, whisperconfig.ini, messages, Message2
; Store all messages in an array
messageArray := [custommessage1, custommessage2]
; Pass the array and message you want to print
^NumPad1::whispermessage(messageArray, 1)
whispermessage(array, index){
; Store the message at the given index in 'finalwhisper'
finalwhisper := array[index]
Msgbox, %finalwhisper% ; Test msgbox
; Print the message
BlockInput On
SendInput ^{Enter}%finalwhisper%{Enter}
BlockInput Off
}
Alternatively, and this is outside the scope of your question, you could load the keys from the .ini file dynamically, meaning you wouldn't have to create new variables each time you added a key/value pair.
Here's how you could do that:
; Read all the key/value pairs for this section
IniRead, keys, whisperconfig.ini, messages
Sort, keys ; Sort the keys so that they are in order
messageArray := [] ; Init array
; Loop over the key/value pairs and store all the values
Loop, Parse, keys, `n
{
; Trim of the key part, and only store the value
messageArray.insert(RegExReplace(A_LoopField, "^.+="))
}
; Pass the array and message you want to print
^NumPad1::whispermessage(messageArray, 1)
whispermessage(array, index){
; Store the message at the given index in 'finalwhisper'
finalwhisper := array[index]
Msgbox, %finalwhisper% ; Test msgbox
; Print the message
BlockInput On
SendInput ^{Enter}%finalwhisper%{Enter}
BlockInput Off
}

Related

variable not accessible after #If directive

var1 := "this works"
#If WinActive("")
d::d
#If
var2 := "this doesn't"
x::
MsgBox, %var1%, %var2%
return
When the hotkey is triggered, it only displays var1, acting like var2 doesn't exist at all.
Why does this happen and what can I do to access var2 from the hotkey?
I can't move var2 up, since my actual code is split accross two files.
You cannot define a variable between or after hotkeys or hotstrings.
Hotkeys/Hotstrings terminate the automatic execution of code lines and the line
var2 := "this doesn't"
never becomes true because is never executed.
A variable has to be defined
in the auto-execute section (top of the script before the first return or hotkey),
or in a hotkey/hotstring,
or in a label (subroutine),
or in a function.

Autohotkey - Attemping to send a specific position of an array

I am attempting to send a specific part of an array
My code is attempting to type a string a specified number of times.
If anyone knows a better way to do this I am open to ideas
#NoEnv ; Recommended for performance and compatibility with future AutoHotkey releases.
; #Warn ; Enable warnings to assist with detecting common errors.
SendMode Input ; Recommended for new scripts due to its superior speed and reliability.
SetWorkingDir %A_ScriptDir% ; Ensures a consistent starting directory.
i = 0 ; Used for a loop
InputBox, x, String, Please enter your string
InputBox, y, No. of times, Please enter the no. of times you want to run the string
k = `n ;used to concatenate strings
z = %x%%k% ;concatenating the input with new lines
F10::
while (i < y) { ;While the loop has run less than the required number of times
i++ ;increment the number of times the loop has run
l = 0 ;used to run a loop a certain number of times
a = StrSplit(%s%) ;Splits the string created earlier
send %a% ;For debug
g = 0 ;used as a value for cycling through arrays
while (l < StrLen(s)) {
h = a[g] ;creates a variable for a specified position in the string
g++
Random, rand, 10, 100 ;creates a variable to wait a random amount of time
r = %rand% ;assigns a random value to a variable for debug
FileAppend, Loop - %h% `n, C:\Users\charl\Desktop\Code\AutoHotkey\Debug.txt ;all file usage is for debug
FileAppend Rand - %r% `n `n, C:\Users\charl\Desktop\Code\AutoHotkey\Debug.txt
send %h% ;sends a character of the original string
Sleep %r% ;sleeps a random amount of time
}
}
I'm sorry the code is such a mess and so hard to udnerstand
The code that works for this specific function is:
InputBox, str, String, Please enter your string
InputBox, amt, No. of times, Please enter the no. of times you want to run the string
Return
F10::
Loop, %amt% {
For k,v in StrSplit(str) {
ToolTip, now %A_Index%
Random, rand, 10, 100 ;creates a variable to wait a random amount of time
;FileAppend %v%`n`n, %A_WorkingDir%\Debug.txt
Send, % v "`n"
Sleep, % rand ;sleeps a random amount of time
}
}

AutoHotKey: Loop doesn't work if a key binding is defined before it

If I define a loop and a key binding after it, it works:
Loop {
IfWinActive, Pixel Dungeon
SetNumLockState, On
WinWaitNotActive, Pixel Dungeon
SetNumLockState, Off
Sleep, 200
}
a::b
But if I define the key binding before the loop, the loop doesn't work anymore:
a::b
; RETURN doesn't help
Loop {
... doesn't work
}
Am I doing something wrong?
a::b is a key remap, if you press "a" it sends "b". AHK replaces a::b internally with two basic hotkeys, so there is an implicit return there. You have to place your code in the autoexecute section above or place it in an hotkey definition like:
+a::
; Loop here
return

How to use counters with variables, error: %%, Autohotkey

In a loop I want to use a counter to use different variables.
The problem is that both use the % symbol: %VAR%
Showing where I get the error %%, Simplified:
textGH1 = Peter
textGH2 = rocks.
i = 1
Loop, 2
{
SendInput {Enter}
Sleep 50
SendInput, %textGH%i%%
Sleep 50
SendInput {Enter}
i++
}
Error: Empty variable reference %%
What I tried:
I have looked up arrays for autohotkey, but I only found
pseudo-arrays using the %COUNTER% as a workaround, similiar to how I
did it. Second, I have assigned a variable in the loop to your current variable:
text := textGH%i%
However this doesn't call the variable, as expected (needed %textGH%i%% again)
I think I understand what you are trying? Swap variables when the counter changes. Easiest way to do this is with an Array.
Here's a solution for you play with:
Define Array. Array's Index values automatically, so Peter is at Index 1 Rocks is at Index 2.
MyArray := ["Peter", "Rocks"]
Loop amount of times as there are Values in your Array.
Loop, % MyArray.MaxIndex() {
SendInput {Enter}
Sleep 50
Send your Array at [Index] A_Index is a built in counter for Loops.
SendInput % MyArray[A_Index]
Sleep 50
SendInput {Enter}
}
Hope this helps.
This should work:
SendInput, %textGH%%i%

How can I use a variable as a key in the associative array in autohotkey?

In autohotkey_L, there is a associative data structure. For example,
hash := {key_hash:"value"}
val:= hash["key_hash"]
MsgBox %val%
But if I want to use a variable as a key to access the value in the assocative array, it fails. For example, the following doesn't work
hash := {key_hash:"value"}
other_val="key_hash"
val:= hash[other_val]
MsgBox %val%
and this doesn't work either:
hash := {key_hash:"value"}
other_val="key_hash"
val:= hash[%other_val%]
MsgBox %val%
** gave me an error: The following variable name contains an illegal character: ""key_hash""
How can I use a variable to access the value in an associative array?
I need this to get the key as an argument in a function.
Alby,
Your variable other_val contained the data: "key_hash" , not what you wanted: key_hash. Just remove the two double quotes and you are fine.
hash := {key_hash:"value"}
other_val=key_hash
val:= hash[other_val]
MsgBox %val%
Or use the assignment (:=)
hash:={key_hash:"value"} ; hash:=Object("key_hash", "value")
other_val:="key_hash"
val:=hash[other_val]
MsgBox, % val