2
votes

I have an AutoHotkey script in which the left-mouse-button is mapped to a function. Part of the function includes simulating a left-button click offset from the actual cursor position. Not surprisingly, this ends up becoming a bit of an infinite loop.

Likewise, there is a handler that traps a key-press and performs some math before passing the key-press on through.

Is there a way perform a click without triggering the click-handler? Similarly, is there a way to send a key-press without triggering the key-press-handler?


Trap() {
  MouseGetPos, x,y
  ;Perform some math with x and y
  Click %x% %y% left ;oops, this causes Trap to get called again
}

LButton:: Trap
2
It looks like some bitter person is revenge-down-voting for some reason.Synetech

2 Answers

3
votes

From the AutoHotkey manual:

$ - This is usually only necessary if the script uses the Send command to send the keys that comprise the hotkey itself, which might otherwise cause it to trigger itself.

That does the trick:

$LButton:: Trap
0
votes

I actually do not see the looping behaviour you describe, so I wonder if there is some other factor at play.

If that really is the problem, then you can use a boolean flag to prevent recursive execution:

isTrapping := false

Trap() {
    global isTrapping

    if isTrapping = true
        return

    isTrapping := true

    MouseGetPos x, y
    ; do some maths with x & y

    Click %x%, %y%

    isTrapping := false
}

LButton::Trap()