On key press, an event is triggered: How to cancel it if another event arrived?

HI

I’ve assigned an event to a Key press inside a textbox.

The problem is that this event is a function which is quite time consuming (it’s a search inside a big document).

So when the next key press arrived, the previous event is still running and it collides with the current event which has just started.

So, I’d like to cancel the running of the function linked to the previous key press, to let the current event be processed.

Is it possible?

What about a common boolean variable - a flag? When entering the function, change the value to the opposite and save it in a local variable. While you complete the actions, check if they are equal and if not, stop the execution.

Public needStop As Boolean
Function onKey1Pressed(...) As ...
Dim canDo As Boolean
   needStop = Not needStop 
   canDo = needStop
   While ...
     ...
    If canDo <> needStop Then Exit Function
   Wend
End Function

Function onKey2Pressed(...) As ...
Dim canContinue As Boolean
   canContinue = needStop
   needStop = Not needStop 
   While ...
     ...
    If canContinue = needStop Then Exit Function
   Wend
End Function

Of course, this will work if you do a search using findFirst/findNext. If you run the findAll search, you will be forced to wait until it finishes - an attempt to interrupt this process can lead to a fall in the office, you should not do such experiments.