Variable hotkeys

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
Nixcalo
Posts: 116
Joined: 06 Feb 2018, 04:24

Variable hotkeys

Post by Nixcalo » 27 Oct 2023, 20:47

I want what I believe is a simple thing but for the sake of me, I cannot get it to work.

I have an script that makes some operations after pressing a hotkey and obtains an array (named Array) with several numerical values. (For example, Array=[2.5,3.3,5.7]
I want the script to create a hotkey for ^1 that sends 2.5 to the screen, ^2 sends 3.3 to the screen, and ^3 sends 5.7 to the screen. Remember, when pressing a particular hotkey (^q in my case), some calculations are performed and the values in the array change. The array can change in values and in size (but they are always numerical).

I have tried something like this (and many variations:

Code: Select all

^q:: ; the hotkey that performs the calculations...blah blah blah It creates an array with, for example,
; the values [2.4,5.6,8.4]
...
 
loop % Array.Length() {
		Hotterkey:=A_Index
	Hotkey, ^%Hotterkey%, etiqueta
	}
MsgBox, Testing ; Thiis stops the script so I can test the new hotkeys at this moment. Would like to remove it eventually.
return

etiqueta: 
SendInput, % Array[Hotterkey]
return
This is the best result I have got, but if the Array has three values, the script correctly creates the hotkeys ^1, ^2 and ^3, but it only shows the contents of the third element of the array (in this example, 8.4). I haven't found a way so when I press ^1 the value "2.4" appears and when I press ^2 the value "5.6" appears. Nope, with ^1, ^2 and ^3 only "6.4" appears... and I don' t know why.

I have tried to use looploop {

Hotkey, !%x%, etiqueta
x++
} until x>Array.Length()
, for For index, value in array
Hotkey, !%index%, etiqueta
, while... tried to use A_Index outside the loop instead of hotterkey, tried to assign the value of A_Index to variables but they don't seem to be transferred to the label etiqueta. Of course, A_Index does not work in the label etiqueta.

Can you help me? I think I must be close, but can't find the solution.

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

Re: Variable hotkeys

Post by mikeyww » 27 Oct 2023, 21:00

Code: Select all

#Requires AutoHotkey v1.1.33
arr := [2.4, 5.6, 8.4]

^1::
^2::
^3::
^4::
^5::
^6::
^7::
^8::
^9::Send % arr[SubStr(A_ThisHotkey, 0)]
If this does not work with your array, then you probably have a problem with how you defined or altered the array.

Post Reply

Return to “Ask for Help (v1)”