Ask Your Question

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

asked 2020-05-14 14:22:58 +0100

linuxyz gravatar image


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?

edit retag flag offensive close merge delete

1 Answer

Sort by » oldest newest most voted

answered 2020-05-14 14:57:32 +0100

JohnSUN gravatar image

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
End Function

Function onKey2Pressed(...) As ...
Dim canContinue As Boolean
   canContinue = needStop
   needStop = Not needStop 
   While ...
    If canContinue = needStop Then Exit Function
End Function
edit flag offensive delete link more


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.

JohnSUN gravatar imageJohnSUN ( 2020-05-14 15:13:58 +0100 )edit
Login/Signup to Answer

Question Tools

1 follower


Asked: 2020-05-14 14:22:58 +0100

Seen: 37 times

Last updated: May 14 '20