Page 1 of 1

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

Posted: 23 Sep 2023, 16:26
by bonobo
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 1031 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.