user254197
user254197

Reputation: 881

AutoHotKey - Run a program once send several times

I am pretty new to AutoHotkey, but I managed it to start my desired program and send an Enter-Key to it, but the probleme here is, the program should only start once and if started it should only receive that enter key, when I press the key stroke again and again it should only send that enter key.

And the program should should stay in the background and not focus after it receives the enter key.

My Code:

#n::
Run F:\V..c.exe
Send {enter}
return

Upvotes: 0

Views: 300

Answers (2)

Amir Hossein Baghernezad
Amir Hossein Baghernezad

Reputation: 4095

you could try:

hasran := false
#n::
if (!hasran) {
    Run F:\V..c.exe
    hasran := true
}
Send {enter}
return

It wont check if that windows exists, but it will only run the program once. then you can navigate to that program and it will only hit the enter key. (if that program is not gui I dont think you can send key events to it)

Upvotes: 0

woxxom
woxxom

Reputation: 73856

Detect if the process exists and start the program minimized, then wait for its window to appear.

#n::
  process, exist, PROGRAM.EXE
  if (errorlevel = 0) {
    run, d:\program.exe, , min
    winwait, ahk_class PROGRAM_WINDOW_CLASS
  }
  controlSend, , {Enter}, ahk_class PROGRAM_WINDOW_CLASS
  ;or use the line below
  ;controlSend, ahk_parent, {Enter}, ahk_class PROGRAM_WINDOW_CLASS
  return

Replace PROGRAM.EXE with the executable name of your program and PROGRAM_WINDOW_CLASS with the window class as seen in the Autohotkey Window Spy utility available in Start menu or in the folder of the Autohotkey (AU3_Spy.exe) or in the right click menu of the Autohotkey tray icon.


Instead of running the program minimized it's also possible to use SW_SHOWNOACTIVATE flag of shellExecute, so you can replace the run, d:\program,, min with this:

dllCall("shell32\ShellExecute", uint,0, uint,0, str,"d:\program.exe", uint,0, uint,0
        ,uint,SW_SHOWNOACTIVATE:=4)

Upvotes: 1

Related Questions