Jump to content

Preventing key default events stops key repeats


maximinus
 Share

Recommended Posts

I'm writing a text entry box. To do this I am having to block some of the key default actions:

var BACKSPACE = 8;
this.input.keyboard.addKey(BACKSPACE);

However, I am finding that doing this also prevents key repeats from firing (so if I hold the backspace down and I do not get repeated keydown events).

I tested in my browser (Firefox 60 / Linux Mint) with plain JavaScript, and calling preventDefault() on the event does not have the same result, so I think this is a Phaser thing.

How then, do I prevent keys from actioned by the browser whilst also retaining key repeats?

Link to comment
Share on other sites

Thanks for replying.

I add ONLY those keys that I don't want to bubble up to the browser in this way:

this.input.keyboard.addKey(keycode);

Then I grab all keydown events:

this.input.keyboard.on('keydown', this.keydown, this);

Then my keydown function does this:

keydown(event) {
    // we start by looking for special keys
    if(event.keyCode === KEYS.BACKSPACE) {
        // is called once only, on the initial keypress
    }
    if(event.keyCode === KEYS.CURSOR_RIGHT) {
        // a keycode NOT added in this way, called once on the initial keydown
        // will repeat as expected when the key is held down
    }
};

 

For comparison, the following code in plain JS will display key repeats for the backspace key:

function keyPressed(event) {
    console.log(event);
    event.preventDefault();
};

document.addEventListener("keypress", keyPressed);

 

(To be precise, if I start the phaser code and then hold down backspace, exactly ONE event is raised.)

Link to comment
Share on other sites

 Share

  • Recently Browsing   0 members

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