Mojtaba Rezaeian
Mojtaba Rezaeian

Reputation: 8746

AHK script loop is not working after using hotkey or replace

using this code:

Loop
{
    WinWait, Untitled - Notepad
    WinClose, Untitled - Notepad
}

I want to keep any new notepad windows closed automatically.

And then I want to add a keyboard shortcut to exit AHK script: Esc::ExitApp

but when I add Esc::ExitApp the loop seems not working and notepad windows will not be closed automatically while code is still running:

Esc::ExitApp  ; ==> this line is causing the loop to malfunction and not work as expected

Loop
{
    WinWait, Untitled - Notepad
    WinClose, Untitled - Notepad
}

Why this is happening and how can I make it work as expected?!

Upvotes: 0

Views: 202

Answers (1)

Relax
Relax

Reputation: 10603

Unlabeled code that follows a return, is unreachable and will never execute.

The hotkey

Esc::ExitApp  

is a single-line hotkey which has an implied return.

To work around it add the hotkey after the loop:

; === auto-execute section (top of the script): ===

Loop ; performs one or more statements repeatedly and is no label
{
    WinWait, Untitled - Notepad
    WinClose, Untitled - Notepad
}

; === end of auto-execute section, because of this hotkey:

Esc::ExitApp

For details see

Auto-execute Section,

Loop

Labels

Upvotes: 1

Related Questions