Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Key Repeat Delay and Release for P5js? #56

Open
Sinfis opened this issue Jan 19, 2024 · 6 comments
Open

Key Repeat Delay and Release for P5js? #56

Sinfis opened this issue Jan 19, 2024 · 6 comments
Milestone

Comments

@Sinfis
Copy link

Sinfis commented Jan 19, 2024

Hello!
I'm interested in developing a p5js game for DOS using this awesome system you have created. :)
However, at the moment there are two problems:

  • The key repeat delay: currently, it seems the only function for keyboard inputs is keyPressed() that, when holding down, executes it once, and after a delay starts repeating it. Is it somehow possible to get rid of this delay, without changing any Windows settings?
  • Detecting key releases: The p5js limitations section says "Key release events work different for Allegro and are simulated for p5js." What does this mean; is it possible to detect key release events for DOjS p5js?
@Sinfis Sinfis changed the title Key release for p5js? Key Repeat Delay and Release for P5js? Jan 19, 2024
@SuperIlu
Copy link
Owner

SuperIlu commented Feb 1, 2024

Hi,

sorry, I completely missed this ticket, seems like I didn't get an eMail for it :(

The long answer first:
DOjS is based on Allegro and all the native APi is directly implemented on top of Allegro functions.
p5js support is a (thin?) layer of Javascript on top of the native DOjS API.

When DOjS is running it calls Setup() once, then it does something similiar to this in a loop:

while(keepRunning) {
  Loop();
  if(Input) {
    Input(currentEvents);
  }
}

The Input() function is called with the current input state as an object:

/**
 * @typedef {object} Event
 * @property {number} x mouse X coordinate.
 * @property {number} y mouse X coordinate.
 * @property {number} buttons mouse buttons, see {@link MOUSE}
 * @property {number} key key code, see {@link KEY}
 * @property {number} ticks event time.
 */
class Event { }

This object is assembled by querying the current Allegro state:

    if (keypressed()) {
        key = readkey();
        ret = ((key >> 8) == DOjS.exit_key);
    } else {
        key = -1;
        ret = false;
    }

So key will be -1 if there is no key pressed or the Allegro key code if a key is pressed (if the key was the "exit key" DOjS will terminate).

p5js has the keyPressed() and keyReleased(), but as you can see above DOjS does not really have a "key release" event. That's why I try to simulate something similar in the Input() and Loop() functions provided by the p5js emulation. See here

exports.Loop = function () {

In short:
I'm not sure if the behavior you describe is caused by p5js-emulation, DOjS, Allegro or MS-DOS.
You seem to run DOjS in a DOSBox on Windows? This adds two more layers of uncertainty :)

I have two suggestions:

  • Try to give me a better explanation on what you want to achieve, maybe a short test program in the p5js editor that works well and where the behavior between p5js and DOjS is different?
  • Check out the native DOjS API and event system and check if it suits your needs better?

@Sinfis
Copy link
Author

Sinfis commented Feb 2, 2024

Thank you for your comprehensive response!

Here's a simple p5js editor script: https://editor.p5js.org/mikkos.koiranen/sketches/HlYKJ3-5v that shows the result I'm after: when holding down arrow keys, the yellow pixel immediately moves as it should do in a game. Also, two arrow keys can be pressed simultaneously, and the pixel moves diagonally.

Here's the same thing executed with a DOjS script, in which when the keys are held down, the movement is done like typing text: first one "step" is taken, then pause, then there are multiple steps as long as the key is held down; that's not an ideal way to move in a game. :) I maybe could avoid this with some coding, but that would need a way to detect the key release. And, it's not possible to press two letters at the same time and move the pixel diagonally.
movepix.zip

@SuperIlu
Copy link
Owner

SuperIlu commented Feb 2, 2024

Right, now I'm getting what you are aiming at.

I tested some stuff and (right now), there is really no way of doing that with DOjS. Neither the native API, nor the p5js emulation are able to provide the info of multiple keypresses.

However: the functionality is available in Allegro: https://liballeg.org/stabledocs/en/alleg006.html#key

I will make this array available in the Javascript world with the next release.

My Idea would be to provide an array with boolean values and you can take the KEY-definitions from func.js to check if a given key is currently down.
I'd update that array every frame, but I have to check how much of a speed impact that would make.
I guess it would be feasible to switch that feature on/off on demand...

I can't really make promises on when I'll manage to try that, but I can give you a test version as soon as I have one if you are interested...

@SuperIlu
Copy link
Owner

SuperIlu commented Feb 3, 2024

Update: this was actually easier than I thought. Please try this unofficial v1.13

DOjS-pre113.zip

@Sinfis
Copy link
Author

Sinfis commented Feb 4, 2024

Hello and thank you very much for your effort!

With the script "keytest.js" the unofficial patch works exactly as it should: the keys can be held down now and they even can be pressed simultaneously allowing diagonal movement.

I'm waiting for the official release, but take your time; at the moment I'm also investigating C programming with the Allegro library and DJGPP for developing DOS games. :)

@SuperIlu
Copy link
Owner

SuperIlu commented Feb 4, 2024

I'll try to make smaller, but more frequent DOjS releases in the future. V1.10 and v1.11 took way too long.

Feel free to contact me on Mastodon or in Discord (see DOjS README.md) if you have questions regarding DJGPP and/or Allegro.

@SuperIlu SuperIlu added this to the v1.13.0 milestone Feb 9, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants