Dynamic Hotstrings

Get help with using AutoHotkey (v2 or newer) and its commands and hotkeys
arout
Posts: 1
Joined: 24 Sep 2022, 13:20

Dynamic Hotstrings

21 Mar 2024, 06:40

Hello AutoHotkey community,
I'm working on a script in AutoHotkey V2, and I've hit a bit of a roadblock. I'm trying to create a dynamic hotstring that expands a typed string such as 'dash10' into a corresponding number of dashes (in this case, 10 dashes "-"). The number of dashes should be dependent on the number included in the typed string (e.g., 'dash5' should result in 5 dashes).
I've attempted a few different methods, but I can't seem to get the functionality to work as intended. Here's what I'm aiming for:

- When I type 'dash' followed by any number (without space), it should automatically expand to that number of dashes.
- The script should be able to handle any number I type after 'dash'.

I'm looking for a way to make this hotstring dynamic so that it's not necessary to write out each variation individually.
Could anyone provide some guidance or a code snippet that would achieve this in AHK V2? Any help would be greatly appreciated!

Thank you in advance for your assistance!
User avatar
kunkel321
Posts: 1062
Joined: 30 Nov 2015, 21:19

Re: Dynamic Hotstrings

21 Mar 2024, 10:16

I'm new to inputHook(), so consider this experimental!!! There are probably other ways to set up the inputhook... I just modeled this one after a script created by @mikeyww :D I recommend having a hyphen after the word "dash." But if you want you can remove it (from lines 7 and 32). Note that you won't see what you are typing until you finish typing the number and press Space or Tab. (I tried to have Enter as an end key too, but it doesn't seem to work (?) ) I made it only do numbers less than 200, but you can change that. If you type a hyphenated word (like dash-kapow), then you just get the word typed out.

Mike, Boiler, etc, Feel free to add suggestions.

Code: Select all

#SingleInstance
#Requires AutoHotkey v2+
; Dash maker.  Experimental !!!!
; based on an inputHook from mikeyww.
esc::ExitApp 

:*:dash-::
{	
	Global thisString := ""
	Global dih := inputHook('I2') 
	dih.OnChar := dih_OnChar
	dih.OnKeyUp := dih_EndChar
	dih.KeyOpt('{Space}{Enter}{Tab}', '+N') ; End keys end the capturing.
	dih.Start
}

dih_OnChar(dih, char)
{	;soundBeep 800
	GLobal thisString .= char
	;ToolTip thisString
}

dih_EndChar(tih, vk, sc) ; Capturing has ended, so evaluate the input. 
{	;soundBeep 1600
	Global thisString
	If IsNumber(thisString) and thisString < 200 ; if the captured input is a number and less than 200.
	{
		Loop thisString ; loop number of times.
			output .= "-"
	}
	Else
		output := "dash-" thisString ; too long, or not a number, so just type content.
	SendInput output
	dih.Stop
	output := ""
	thisString := ""
}
ste(phen|ve) kunkel
User avatar
kunkel321
Posts: 1062
Joined: 30 Nov 2015, 21:19

Re: Dynamic Hotstrings

21 Mar 2024, 13:38

Okay, a more fun version... Type "dash," then one of the characters -+#*~+ or x, then a digit, then Space.

Another interesting thing... AHK gurus, please note the IF Else statement near the bottom. It seems like the code should work the same with or without the {braces}, yes?? But try this: If you type the trigger, then a non-digit (or digit > 199), you get the trigger typed out. For example, typing dash-hello{Space} yields "dash-hello." HOWEVER, if you comment-out the curly braces and try it, the Else is skipped (var 'output' is not set to anything). Mystery solved! See below. :lol:

Code: Select all

#SingleInstance
#Requires AutoHotkey v2+
; Dash maker.  Experimental !!!!
; based on an inputHook from mikeyww.
esc::ExitApp 

For item in ['-','=','#','*','~','+','x']
	HotString(':*:dash' item, startHook.Bind())
startHook(*)
{	Global thisString := ""
	Global dih := inputHook('I2') 
	dih.OnChar := dih_OnChar
	dih.OnKeyUp := dih_EndChar
	dih.KeyOpt('{Space}{Enter}{Tab}', '+N') ; End keys end the capturing.
	dih.Start
}

dih_OnChar(dih, char)
{	GLobal thisString .= char
}

dih_EndChar(tih, vk, sc) ; Capturing has ended, so evaluate the input. 
{	Global thisString
	output := ""
	thisDash := '{' subStr(A_ThisHotkey, -1) '}' ; <--- The curley braces allow us to use # or +. 
	If (IsNumber(thisString) and thisString < 200) { ; if the captured input is a number and less than 200.
		Loop thisString ; loop number of times.
			output .= thisDash
	}
	Else {
		output := "dash" thisDash thisString ; too long, or not a number, so just type content.
	}
	SendInput output
	dih.Stop
	thisString := ""
}

Last edited by kunkel321 on 21 Mar 2024, 16:34, edited 1 time in total.
ste(phen|ve) kunkel
User avatar
boiler
Posts: 16984
Joined: 21 Dec 2014, 02:44

Re: Dynamic Hotstrings

21 Mar 2024, 14:15

@kunkel321 - You're right. Seems like an issue. You would think the following code would display the MsgBox in both v2 and v1, but it only does in v1:

Code: Select all

if (2 > 3)
	loop 1
		SoundBeep
else
	MsgBox
Descolada
Posts: 1141
Joined: 23 Dec 2021, 02:30

Re: Dynamic Hotstrings

21 Mar 2024, 14:38

@boiler a quote from Loop documentation:
The loop may optionally be followed by an Else statement, which is executed if Count is zero.
Your example is interpreted as

Code: Select all

if (2 > 3) {
	loop 1
		SoundBeep
	else
		MsgBox
}
User avatar
boiler
Posts: 16984
Joined: 21 Dec 2014, 02:44

Re: Dynamic Hotstrings

21 Mar 2024, 16:03

Ah, thanks, @Descolada. Good to know!
User avatar
kunkel321
Posts: 1062
Joined: 30 Nov 2015, 21:19

Re: Dynamic Hotstrings

21 Mar 2024, 16:28

Yes -- Good info. Thanks.
ste(phen|ve) kunkel

Return to “Ask for Help (v2)”

Who is online

Users browsing this forum: No registered users and 38 guests