Travis Heeter
Travis Heeter

Reputation: 14084

How to write a script that creates hotkeys?

I have a lot of little hot keys defined, such as:

; Open CMD
#c::
    Run, cmd.exe
    WinWait, ahk_exe cmd.exe
    WinActivate
Return

I'd like to build a function that takes the exe and hot key, and it will bind the app with that hot key. Here's what I have so far:

bind_exe_to_hotkey(exe,hotkey)
{
    run_label:
        Run, %exe%
        WinWait, ahk %exe%
        WinActivate
    Return

    HotKey, %hotkey%, run_label
}

bind_exe_to_hotkey("cmd.exe","#c")

However, this just opens a command window. What am I doing wrong? Is there an easier/better way to accomplish this?

Upvotes: 0

Views: 77

Answers (1)

Jim U
Jim U

Reputation: 3366

Binds key to a function that handles launching an executable:

#c: launch("cmd.exe")
#n: launch("notepad.exe")

launch(exe)
{
   Run, %exe%
   WinWait, ahk %exe%
   WinActivate
}

Upvotes: 1

Related Questions