KeyDown Event semaphore

The keyDown event of a Canvas call several methods according to the key selected.

Some procedures may take a second or two, I would like to prevent any additional keyDown until the methods have finished.

The first way I’ve chosen is to put a variable (semaphore) that is triggered with the keyDown happens and reset when all the code is finished.

But probably Xojo has build in a better way to prevent this, can someone give me some light?

You shouldn’t be experiencing re-entrancy in your code. Ie., in normal operations, nothing else should happen until your original KeyDown event finishes, regardless of how many methods you call. Unless you are spawning threads to do the additional methods, but you didn’t mention that.

or calling DoEvents and then all bets are off

1 Like

This is the code.

if an arrow key is pressed several times quickly, for example de PageDown) then, the window shows the first page down, then the second and so on, and no way to stop it until all have finished,

I’d like the PageDown/up/right or left is not executed if there is still the latest one redrawing.

Case kRightArrow
  PageLeft()
Case kLeftArrow
  PageRight()
Case kUpArrow
  PageUp()
Case kDownArrow
  PageDown()

What you’re seeing is the system queuing the keypresses and passing them one by one to your app. There’s really no way for you to detect that as your KeyDown event will finish the first one completely before the next one hits your app.

1 Like

Now that I think about it, I have dealt with this in the past by recording the Microseconds at the end of the event and comparing that at the top of the event and if they happen “too fast”, disregard them.

Theres probably a declare or MBS function you could call to discard any repeated events
Or just empty the apps event queue of all other keyboard &/or mouse events

However, that could have unintended side effects as far as users are concerned

Thanks, I’ll try this.