Jump to content

Key press event / non-repeat keydown event?


The Geek on Skates
 Share

Recommended Posts

Hey, what's up?  New guy here. :)

I've been building a platformer with Phaser 3, and it's been coming along great!  But now I'm working on the menu, and I'm having a hard time figuring out how to make it less "slippery".  I've been pouring over Google for the past 3 hours and couldn't find anything about event repeating, handling different events, etc.  Everything I found was for Phaser 2 and Phaser "CE" (guess they like to follow Microsoft's random naming of OSes lol).  Anyway, it appears they're still writing the Phaser 3 documentation, so my only other idea has been to poke around in the JS console, try a bunch of random stuff, and see what sticks.  So far, nothing I've tried has worked, so I figured I'd join and ask the experts. :)

The problem is that the only way to handle keyboard events - at least the only one I'm aware of - is by using the "isDown" property of the keyboard object.  This is like the JS "onkeydown" event, but what I'm looking for is more like "keypress".  I also tried implementing a sort of keyboard-state-tracking system, with Booleans for each key (which I've done in other frameworks like Allegro (in C++) or PyGame (in Python), but that didn't work here.  I even tried plain old JS event listeners.  No matter what I do, the unstoppable key repeat makes cursor move way, way too fast to even bother trying to create a menu at all.

Sorry if this is info overload, but I've got a TON of questions here...

  1. is there an "isPressed" property or similar?  Or a "noRepeat" setting someplace?
  2. If not, what other options are there?
  3. Is Phaser 3 just not stable yet?  Should I consider downgrading to 2 or "CE"?  Usually I'm totally down with playing with repos that are still in development as long as it has some kind of docs, but undocumented APIs are about as fun as trying to play Super Mario with your toes. :)
  4. Also, what other keys are available besides up/down/left/right?  I've tried things like .space, or .vk_space, or .VK_SPACE but again nothing works.

Thanks in advance for your help, and have an awesome weekend! :)

PS: As a side note, is there any more keyboard-friendly version of this WYSIWYG?  It doesn't seem to support pasting, sometimes the arrow keys and other keyboard navigation techniques don't work, and It's really just not that good.  Next time I think I might just write my question in a text file and attach it lol... this thing makes Notepad look cutting-edge. :)

example.js

Link to comment
Share on other sites

Wow, thanks samme!  :)

I used some of the code from first page, and now my menu is working great.  But more importantly, you just gave me two great resources for future questions.  With so many pages on phaser.io saying "this is for version 2.__, version 3 coming soon" I didn't think they had any documentation anywhere else.  So I'm definitely gonna bookmark that GitHub repo (lol)!  And I really should have thought to Google examples - I do that all the time with work-related code (duh).

Anyway, thanks again, and enjoy the rest of your weekend.

PS: Cool avatar. :)

Link to comment
Share on other sites

  • 4 months later...

You can also manage your event by yourself :

// from scene load function

let keys = new Set();

this.input.keyboard.on('keydown', (event) => {
    if (!keys.has(event.code)) {
        keys.add(event.code);
        this.input.keyboard.emit(`keypress_${event.code}`);
        // or / and
        this.input.keyboard.emit(`keypress`, event.code);

    }
});

this.input.keyboard.on('keyup', (event) => {
    keys.delete(event.code);
    this.input.keyboard.emit(`keyrelease_${event.code}`);
    // or / and
    this.input.keyboard.emit(`keyrelease`, event.code);
});

 

Cheers :-)

Link to comment
Share on other sites

 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...