50

Yesterday while debugging my AutoHotkey script, I accidentally triggered an endless loop of MouseMove and MouseClick events. Every 0.5 seconds my mouse would click random parts of the screen.

After unsuccessfully trying to terminate AHK with the task manager, I ended up turning off my computer to end the script.

How can I stop an active AutoHotkey script?

Stevoisiak
  • 23,794
  • 27
  • 122
  • 225

8 Answers8

68

Add an emergency exit hotkey

The most reliable method of ending an active script is to pre-emptively include an emergency ExitApp hotkey. A common practice is to place the following at the bottom of any script.

Esc::ExitApp  ; Exit script with Escape key

You can also set hotkeys to pause, suspend, or reload your script.

^!p::Pause    ; Pause script with Ctrl+Alt+P
^!s::Suspend  ; Suspend script with Ctrl+Alt+S
^!r::Reload   ; Reload script with Ctrl+Alt+R

Log off

On Windows 10/8/7/Vista, you can quickly log off with the keyboard shortcut Ctrl+Alt+Delete, followed by Alt+L.

This is because pressing Ctrl+Alt+Delete opens a special window which cannot be manipulated by programs like AutoHotkey.

End with taskbar icon

If you have control of the keyboard and mouse, you can end the script by right-clicking AutoHotkey's green H icon in the taskbar and selecting "Exit"

Exit script from taskbar

End all active scripts with AHKPanic()

For a more generic solution, AHK user None wrote AHKPanic(), a method which can pause, suspend, or kill all other running scripts. (Optionally ends the script that called it)

AHKPanic(Kill=0, Pause=0, Suspend=0, SelfToo=0) {
DetectHiddenWindows, On
WinGet, IDList ,List, ahk_class AutoHotkey
Loop %IDList%
  {
  ID:=IDList%A_Index%
  WinGetTitle, ATitle, ahk_id %ID%
  IfNotInString, ATitle, %A_ScriptFullPath%
    {
    If Suspend
      PostMessage, 0x111, 65305,,, ahk_id %ID%  ; Suspend. 
    If Pause
      PostMessage, 0x111, 65306,,, ahk_id %ID%  ; Pause.
    If Kill
      WinClose, ahk_id %ID% ;kill
    }
  }
If SelfToo
  {
  If Suspend
    Suspend, Toggle  ; Suspend. 
  If Pause
    Pause, Toggle, 1  ; Pause.
  If Kill
    ExitApp
  }
}
Stevoisiak
  • 23,794
  • 27
  • 122
  • 225
13

If you want to get fancy and stop it from the Windows command prompt,

Stop all autohotkey scripts with:

taskkill /im "autohotkey.exe"

Stop specific script with:

wmic process where "commandline like '%%MyScript.ahk'" delete

As of Windows 10 21H1, WMIC is deprecated, so the following PowerShell script will do the equivalent:

Get-WmiObject win32_process -Filter "commandline like '%%MyScript.ahk""'" |
    ForEach-Object { $_.Terminate() }
micsthepick
  • 562
  • 7
  • 23
PBeezy
  • 1,222
  • 2
  • 17
  • 26
  • 1
    im very interested in getting this to work. I am trying to get a .bat script to stop one ahk script form running, but your example "Stop specific script with" doesnt seem to work. – Kalamalka Kid Sep 25 '19 at 08:28
  • @KalamalkaKid Make sure you include the %% in your command. So if your script is called Run.ahk, the string should be '%%Run.ahk'. If you type wmic process in a command prompt, you'll see all the processes running, including your hotkey script, so you should be able to see what you need to invoke to make it stop. – PBeezy Sep 27 '19 at 17:42
  • The thing is is I do not want to stop all scripts I only want to stop a certain script – Kalamalka Kid Sep 27 '19 at 17:48
  • 1
    Right, so whatever your script is called, put that in the string between the single quotes, including the percent signs. Replace MyScript in my example with the name of Your Script. – PBeezy Sep 27 '19 at 18:27
  • 1
    The example code is in my answer. `wmic process where "commandline like '%%MyScript.ahk'" delete` If it's not working, it might be some other problem, like the bat file not working or permissions issue or something. I recommend you ask a friend, or your boss, or your teacher to help you with it. – PBeezy Oct 05 '19 at 00:34
  • Thanks.. quick question... does the delete portion of this code delete the file or simply stop the process from running? – Kalamalka Kid Oct 05 '19 at 02:48
  • It stops, or deletes, the process, NOT the file. – PBeezy Oct 05 '19 at 13:26
2

If you want to close a specific window without doing a loop, I have created the following:

DetectHiddenWindows, On

path = %A_Desktop%\Test.ahk - AutoHotkey v1.1.26.01

path2 = %A_Desktop%\Test2.ahk - AutoHotkey v1.1.26.01

WinClose, %path%

WinClose, %path2%

**NOTE 1 : ** the file name + " - AutoHotKey blah blah blah" (This is the window name for hot key when you open the window hidden on your bottom right corner close to the clock)

**NOTE 2 : ** the file names have to be case sensitivity.

LuFFy
  • 8,799
  • 10
  • 41
  • 59
2

You could also make your script #SingleInstance force and watch for a "-quit" argument or something.

#SingleInstance force
#Persistent

If A_Args[1] == "--stop"
    ExitApp

; just some random stuff for the script to do
count := 0
Loop,
{
    count++
    Tooltip, %count%
    Sleep, 200
}

So you run the same script again but with the argument. And BAM its gone.

Just for completeness. @Stevoisiak's answer is already great for the purpose requested.

ewerybody
  • 1,443
  • 16
  • 29
1

I was also looking for how to close one specific running script from another autohotkey script, so I tried cristivaldo's solution but it wasn't quite working for me, so I changed it a bit.

I added SetTitleMatchMode, 2 so it matches window titles with partial matches, so you don't have to type exactly the full window title (that way it will still match even if the autohotkey version number changes). I also removed the path, and variables.

Here's an example to stop/close a specific script (just change examplescriptfile.ahk to the filename of the running autohotkey script you wish to stop):

DetectHiddenWindows, On
SetTitleMatchMode, 2

WinClose, examplescriptfile.ahk
gamingexpert13
  • 346
  • 5
  • 6
1

1.go to the windows tray
2.right-click on the "H" sign
3.click on "suspend hotkeys" or "exit"

This is the easiest and the most simplest way to stop autohotkey scripts

but dont click on pause script, otherwise remapped keys will be shutdown

also, if you have suspended the hotkeys, the "H" will become a "S"

Hope this helped.

Anonymous
  • 168
  • 12
0

I think you can also do CTRL+ALT+DEL

mbourd
  • 35
  • 4
0

In the event that you accidentally trigger an endless loop of MouseMove and MouseClick events in your AutoHotkey script, fear not, for there are several methods at your disposal to stop this rogue script in its tracks. Here's how to do it using the Task Manager on a Windows-based operating system:

Step 1: Begin by pressing the Ctrl+Alt+Del keys in succession to invoke the Windows Security screen.

Step 2: Once the Windows Security screen appears, proceed to select the "Task Manager" option, but beware, this operation may take several seconds to load. So, practice patience.

Step 3: Next, take note of the number of icons that appear before the Task Manager icon in the taskbar. Assume that there are five icons before the Task Manager icon; therefore, the taskbar number for the Task Manager icon would be 6 (5 + 1).

Step 4: Now, it's time to press Windows+Ctrl+(Task Manager icon taskbar number) to bring the Task Manager to the forefront of your display. For instance, if the Task Manager icon is the sixth icon in the taskbar, then press Windows+Ctrl+6.

Step 5: To get the Task Manager window as the active application, you'll have to act fast and click on it quickly; otherwise, you'll lose control of your computer again. Once it's active, you should regain control of your system.

Step 6: As you search for the AutoHotkey process in the Task Manager, keep in mind that it's usually found under the "Processes" or "Details" tab, and it may be called "AutoHotkey.exe" or "AutoHotkey Unicode 64-bit". If you're having trouble locating it, try sorting the list by name or CPU usage, and you'll eventually stumble upon it.

Step 7: In the final step, right-click on the AutoHotkey process and select the "End task" option to terminate the script.

To ensure that this issue doesn't occur in the future, take measures to prevent infinite loops by incorporating safeguards into your code and thoroughly testing your scripts before running them. You can also consider adding a hotkey to your scripts to allow you to easily terminate them in case of issues. For example, you can use the "Esc" key or a custom hotkey to exit the script.

I hope that this question, and I understand that these instructions will be a little harder to understand than the previous falsely-flagged and deleted answer, but by explicitly making the text harder to understand reduces the probability of false AI generated flags.

Y K
  • 25
  • 8