Clarification about simulating key press events

I’m aware that this is an event specifically for objects with the platformer behavior. However, I’m wondering if there is any technical reason there can’t be a feature that allows you to simulate ANY key press regardless of object behavior. The primary reason for me is that it can be very useful for creating a kind of “Autopilot” system or for automated testing for debug purposes. And I’m sure a few other uses as well.

My understanding as far as I remember:
Javascript (and Electron) doesn’t allow for direct interaction with the OS nor Hardware layer (beyond read access).

The other events (for “Key pressed”) are listening on the hardware layer, so there’s no way to force a fake key press in that manner.

OK, and I’m assuming there’s no clever workaround to do that either?

I’m not going to say it is impossible, but I’m just not aware of any, no.

Hi, I could think of one way, using boolean variables :

Your keyboard actions would toggle Boolean variables to true, and then the Boolean variables would be used to control the game. For example you press « a » key to make a character jump. There would be 2 events about it :

  • if a key pressed, then set Boolean variable « key.pressed.a » to true
  • if Boolean variable « key.pressed.a » is true, then character jumps

To do so :
At the beginning of which frame (so on top of your code), do 2 things :

  • toggle to false all your key Boolean variables (the action « clear variable “key” » should work, I hope it does, but I’m not sure.)
  • for each key that you care about, write an event like « if a key pressed, then set Boolean variable « key.pressed.a » to true »

This was the part at the beginning of your frame.

Then, everywhere else in your code where you would usually use key (pressed or released), instead, just use your Boolean variables.

That’s what I can think of. I guess there’s no reason it wouldn’t work.
With this system you could simulate any command, just by toggling Boolean variables.

I’ll actually give this a go and see if it works, I’ll report back with an edit. (Edit: Yep, this is a useable workaround. This should be pinned in a sub forum called “Workarounds” because of how useful this is lol. In fact, I think this could be the basis for an extension to make it easier for everybody.)

1 Like

Great to hear it works fine :))
I suppose it might also help when you want to let the player change the command in the game settings.