My working phaser game is accept physical keyboard input as expected, no worries.
[I am not a company, just hobbyist]
However when i dispatch a keyboard event from a html button click (which is picked up OK by a listener) I am not seeing phaser pick up the event and respond with appropriate update. The same listener fires when the key is physically pressed or the event dispatched (virtual press) but one works and not the other. Do I have to inject the event somehow, I assumed it would work ?
//example from the code which respond to physical key press
this.cursors = this.input.keyboard.createCursorKeys();
this.input.keyboard.isDown(Phaser.Keyboard.Z)
// fire event after button press
this.$emit(“fire”, {fire: dispatchEvent(new KeyboardEvent(‘keydown’, {‘key’: ‘z’}))});
//listener that does hear it
$(this).keydown(function(event) {
if (event.key === ‘z’) {
console.log(‘z pressed!’);
}
//nothing happen in phaser but perfect when physical key press
Hi,
I tried to do a similar things…
It seems browsers don’t like this for security reasons. So I gave up on the idea …
But this stackoverflow post can perhaps help you:
The listener fires in both cases, one of which the “virtual version”, is found in the link you posted(i tried many variations on this theme and Phaser only works in one case. I understand the security comment however I cannot believe the final goal is not possible "html buttons that you can mouse click or touch (mobile screen) that interact with phaser like keyboard.
Thanks to all those who try to assist me.
Eventually I found this link which I have certainly got to work.
//in the create function per item, reference the button ID
var fireRef = document.getElementById(‘fire’);
fireRef.addEventListener(‘click’,this.fireBtn.bind(this));
//new/standalone
fireBtn: function () {
this.fire();
},
Perfect I can continue on to the next experiments !