Search code examples
autoit

Remap without recursion


I'm very new in AutoIt, worked before (a lot) with AutoHotkey.

Here is my attempt to slightly rewrite one of the scripts from AutoIt docs. The only part I've changed is the bottom one (Tab key).

What I want to achieve: If current window is Notepad, then send Ctrl-Tab instead of Tab. Otherwise, send normal Tab.

Well, I understand, why this code leads to recursion, but how I can avoid it?

#include <MsgBoxConstants.au3>

; Press Esc to terminate script, Pause/Break to "pause"

Global $g_bPaused = False

HotKeySet("{PAUSE}", "HotKeyPressed")
HotKeySet("{ESC}", "HotKeyPressed")
HotKeySet("{TAB}", "HotKeyPressed")

While 1
    Sleep(100)
WEnd

Func HotKeyPressed()
    Switch @HotKeyPressed
        Case "{PAUSE}"
            $g_bPaused = Not $g_bPaused
            While $g_bPaused
                Sleep(100)
                ToolTip('Script is "Paused"', 0, 0)
            WEnd
            ToolTip("")

        Case "{ESC}"
            Exit

        Case "{TAB}"
            If WinActive("[CLASS:Notepad]") Then
                Send("^{TAB}")
            Else
                Send("{TAB}")
            EndIf

    EndSwitch
EndFunc

Solution

  • Temporarily disable the hotkey:

    Case "{TAB}"
        HotKeySet("{TAB}")  ;Cancel (or unregister) the hotkey
        If WinActive("[CLASS:Notepad]") Then
            Send("^{TAB}")
        Else
            Send("{TAB}")
        EndIf
        HotKeySet("{TAB}", "HotKeyPressed") ;re-enable hotkey
    EndSwitch