Dynamic Hotkeys from Inifile Topic is solved

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
diafol
Posts: 5
Joined: 29 Sep 2022, 09:04

Dynamic Hotkeys from Inifile

Post by diafol » 29 Sep 2022, 09:17

Hi all, bit of a noob, but am trying to make some dynamic keys from an inifile. The inifile has the structure:

Code: Select all

[Union]
#a=â
61={U+00E2}
#c=ç
63={U+00E7}
#d=δ
64={U+03B4}
#e=ê
65={U+00EA}
#g=°
67={U+00B0}
(etc)

So I'm trying to get the data from the ini to dynamically create hotkeys (AltGr with the key listed (hex values, e.g. 61 = a) which will then send â as output ({U+00E2}). My reason behind this is to allow colleagues to modify their hotkeys via the ini file (which is another part of the script - which works).

#NoEnv
#SingleInstance force
#Persistent

Code: Select all

ProcessDirect()
{
	iniread, varSection, AltCym.ini, Union
	myArray := []

	loop, parse, varSection, `n
	{
		if SubStr(A_LoopField, 1, 1) <> "#"
		{
			data := StrSplit(A_LoopField, "=")
			myArray.Push({key:data[1],value:data[2]})
		}
	}

	for each, item in myArray
	{
		mykey:= chr(item.key)
		myval:= item.value
		Hotkey <^>!%mykey%, send %myval% ;YIKES!!
	}
	return
}
The function works up to the last line of the for each loop. Then I'm stuck. Any help would be gratefully appreciated.

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

Re: Dynamic Hotkeys from Inifile

Post by mikeyww » 29 Sep 2022, 14:57

I'm not sure if this helps, but :arrow: Hotkey uses a label rather than a command. See the documentation.
The name of the label whose contents will be executed (as a new thread) when the hotkey is pressed. If not a valid label name, this parameter can be the name of a function, or a single variable reference containing a function object.

diafol
Posts: 5
Joined: 29 Sep 2022, 09:04

Re: Dynamic Hotkeys from Inifile

Post by diafol » 30 Sep 2022, 17:26

Thanks mikey. Yes I was aware of the label. I also read something about a function object? Anyways, I was hoping somebody could advise an approach for setting hotkeys dynamically from a list (inifile).

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

Re: Dynamic Hotkeys from Inifile  Topic is solved

Post by mikeyww » 01 Oct 2022, 05:44

Code: Select all

#SingleInstance Force
Global char := {}
processDirect("AltCym.ini", "Union")
Return

Key:
Send % char[SubStr(A_ThisHotkey, 0)]
Return

processDirect(ini, section) {
 IniRead, section, %ini%, %section%
 Loop, Parse, section, `n
 { data := StrSplit(A_LoopField, "="), char[c := Chr(Format("{:i}", "0x" data.1))] := data.2
   Hotkey, <^>!%c%, Key, On
 }
}

diafol
Posts: 5
Joined: 29 Sep 2022, 09:04

Re: Dynamic Hotkeys from Inifile

Post by diafol » 01 Oct 2022, 17:55

Oh my goodness. Thank you v much mikey. From reading your reply, I can see how out of my depth I am/was! I shall try it when I get back to the laptop. Thanks again :)

diafol
Posts: 5
Joined: 29 Sep 2022, 09:04

Re: Dynamic Hotkeys from Inifile

Post by diafol » 02 Oct 2022, 03:32

The code worked perfectly. Thank you. :D

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

Re: Dynamic Hotkeys from Inifile

Post by mikeyww » 02 Oct 2022, 06:54

Good to hear. Enjoy! :)

Post Reply

Return to “Ask for Help (v1)”