Html5Overview Posted October 25, 2018 Share Posted October 25, 2018 Hi everyone, I'm creating a game in Phaser 3. I'd like to ask you how do I read a single character key from a keyboard event I've pressed. this.input.keyboard.on('keydown', function(input) { inputText.setText(input.keypress); }, this) Link to comment Share on other sites More sharing options...
Html5Overview Posted October 25, 2018 Author Share Posted October 25, 2018 I'm sorry, I have found a solution. this.input.keyboard.on('keydown', function(input) { inputText.setText(input.key); }, this) Paul Bonneau and Befive.Info 2 Link to comment Share on other sites More sharing options...
s4m_ur4i Posted October 26, 2018 Share Posted October 26, 2018 Hey I'd like to share my controls class with you: Maybe it is helpful for your purpose Everytime const controls = new Controls -> if(controls.on('keyname')) is done, you get a true or false. class Controls { constructor(scene) { this.scene = scene; this.enabled = true; this.keyboard = this.scene.input.keyboard.createCursorKeys(); this.key = this.scene.input.keyboard.addKeys({ 'TAB': Phaser.Input.Keyboard.KeyCodes.TAB, 'ESC': Phaser.Input.Keyboard.KeyCodes.ESC, 'Space': Phaser.Input.Keyboard.KeyCodes.SPACE, 'X': Phaser.Input.Keyboard.KeyCodes.X, 'One': Phaser.Input.Keyboard.KeyCodes.ONE, 'Two': Phaser.Input.Keyboard.KeyCodes.TWO, 'F': Phaser.Input.Keyboard.KeyCodes.F, 'C': Phaser.Input.Keyboard.KeyCodes.C, 'W': Phaser.Input.Keyboard.KeyCodes.W, 'M': Phaser.Input.Keyboard.KeyCodes.M, 'I': Phaser.Input.Keyboard.KeyCodes.I, 'A': Phaser.Input.Keyboard.KeyCodes.A, 'S': Phaser.Input.Keyboard.KeyCodes.S, 'D': Phaser.Input.Keyboard.KeyCodes.D, 'E': Phaser.Input.Keyboard.KeyCodes.E, 'Enter': Phaser.Input.Keyboard.KeyCodes.ENTER }); } on(actionName) { switch (actionName) { case 'left': return (this.keyboard.left.isDown || this.key.A.isDown) && this.enabled; case 'right': return (this.keyboard.right.isDown || this.key.D.isDown) && this.enabled; case 'down': return (this.keyboard.down.isDown || this.key.S.isDown) && this.enabled; case 'up': return (this.keyboard.up.isDown || this.key.W.isDown) && this.enabled; case 'X': return this.key.X.isDown && this.enabled; case 'Y': return this.key.C.isDown && this.enabled; case 'M': return this.key.M.isDown && this.enabled; case 'I': return this.key.I.isDown && this.enabled; case 'jump': return (this.key.Space.isDown || this.key.F.isDown) && this.enabled; case 'B': return (this.key.E.isDown || this.key.Enter.isDown) && this.enabled; case 'LT': return this.key.One.isDown && this.enabled; case 'RT': return this.key.Two.isDown && this.enabled; case 'MENU': return this.key.ESC.isDown && this.enabled; } } } You can achieve the same with events, triggering a: this.scene.event.emit('key_<name>'); on a keypress every time a key is pressed. Link to comment Share on other sites More sharing options...
Recommended Posts