Best way to simulate or trigger button onClick event when return key is pressed or released

For context: this is to help my accessibility issue. I currently have a way to trigger events when a user tabs to a sprite (button) and hits the return key. But it is essentially a race condition. Please let me know if there is a better way to do this.

How I currently get it to work.

  1. In javascript I create a javascript global variable called globalAriaLabel

  2. In GDevelop I create gdevelop global variable called currentAriaLabel. It is constantly updated with the text of the javascript global variable from step 1 using an empty condition.

  3. every button created must have an object variable called “accessibleButtonText”.

  4. I use Pixi.js’ accessibility feature to render that sprite as an accessible button.

  5. I register an onclick event for each button that looks like:
    Screen Shot 2022-05-09 at 12.55.13 PM

  6. Notice the click function does three things. First it updates the javascript global variable. Then, it changes the focus back to the canvas. Lastly, it simulates the Return key being pressed on the canvas.

  7. Lastly to check which buttons’ events should be triggered, I check to see if the currentAriaLabel global variable from step 2 matches the accessibleButtonText object variable from step 3.

So all of this does work but it is not ideal at all. Specifically the race condition of updating variables. Right now when someone presses enter when one of buttons/sprites currently has focus (triggering my custom click event): I first (a) update the javascript global variable, then (b) I shift focus to the canvas, then I (c) simulate someone pressing the return key on the keyboard. All while hoping that somehow the GDevelop global variable gets updated in time but only after (a) but before (c).

All that comes to mind about your race issue is that custom behaviors allow you to choose the time of execution…

You might find more techie help on a dev help board like StackOverflow. :confused:

You could also try our Discord, we have a javascript channel.

Thank you! The doStepPreEvents in custom behaviors seems to be an okay workaround. But is there a way to get the same functionality without it being attached to an object? I guess it would be like just putting the event at the very top of every event sheet.

Idk if stackoverflow will help since this is pretty GDevelop specific, but I’ll definitely check out the discord. Thanks!!

I said custom behaviors, but you have it also in the custom functions where, I suppose, you don’t need it attached to any object.