Assign a non-AHK shortcut to launch your main AHK script

Put simple Tips and Tricks that are not entire Tutorials in this forum
bonobo
Posts: 75
Joined: 03 Sep 2023, 20:13

Assign a non-AHK shortcut to launch your main AHK script

Post by bonobo » 23 Sep 2023, 16:26

Did you know that windows allows you to assign a "shortcut to a shortcut"? That is, a system-wide keyboard shortcut to launch an .lnk shortcut file (as long as you place the .lnk file in the Start Menu folder)?
image.png
image.png (19.16 KiB) Viewed 897 times
If, like me, you sometimes need to quit then re-launch (rather than simply reload) AHK, you can put something like this in the autoexec section of your main script. The example code allows the use of "ctrl+alt+q" to launch your main script file even without AHK already running. (And if you have an AHK-defined hotkey to quit the script, you can reuse the same hotkey for launching the script, so the overall effect of the hotkey is to toggle the running/not-running status of the script itself)

Code: Select all

CreateShortcutToScript("q")
CreateShortcutToScript(ShortcutKey){
	ShortcutPath := A_StartMenuCommon "\" A_ScriptName ".lnk"
	if !FileExist(ShortcutPath)
		FileCreateShortcut(A_AhkPath, ShortcutPath, A_WorkingDir, '"' . A_ScriptFullPath '"',,,ShortcutKey,,)
}

^!q::ExitApp()
Limitations: FileCreateShortcut currently can only assign ctrl+alt shortcuts. If you want to assign ctrl+alt+shift+... shortcuts (as seen in the screenshot above), you need to manually configure it in the Properties window.

Return to “Tips and Tricks”