Areg Posted July 7, 2014 Share Posted July 7, 2014 I know from examples that there are ways to capture keyboard events in the update() function of the game( that is, polling of events). But is there a way in phaser to register listeners to all keys/specific keys of keyboard. For example I created on-screen keyboard for my game, but I also want to listen to the physical keyboard. Thanks Link to comment Share on other sites More sharing options...
lewster32 Posted July 7, 2014 Share Posted July 7, 2014 This was answered in this post: http://www.html5gamedevs.com/topic/7256-generic-key-input/?hl=%2Bkeyboard+%2Bevent#entry43265 Be sure to read some of the replies as they include tips for ensuring cross-browser compatibility. Link to comment Share on other sites More sharing options...
quarks Posted July 7, 2014 Share Posted July 7, 2014 The idiom I see in most of the examples for controls is polling in update(). Is there a reason to prefer this to a more event driven approach? Link to comment Share on other sites More sharing options...
lewster32 Posted July 8, 2014 Share Posted July 8, 2014 The example I provided is an event-based approach, but it depends what you're trying to do. For discreet inputs like taps, clicks or single keypresses events are the way to go, but for constant inputs like movement, where you typically hold an input down as long as the action should be relevant, then polling works better than having potentially messy hold and release events. Link to comment Share on other sites More sharing options...
quarks Posted July 8, 2014 Share Posted July 8, 2014 Is the method with hold and release events just conceptually messier, or is there something about those events are handle in Phaser or the host environment that creates problems? Link to comment Share on other sites More sharing options...
lewster32 Posted July 8, 2014 Share Posted July 8, 2014 I think it's mainly a conceptual thing. It takes less code and is more reliable to have an 'isDown' boolean polled in a game loop for actions which require you to hold the button for an unspecified length of time. The same is also true for events to be fired when something 'happens' rather than when something is 'happening' - if you already have a game loop running, then it seems wasteful to have an event like 'onHold' which is fired 60 times per second, or worse, at an arbitrary frequency. In that situation it's much better to have a function called once when the input is activated. Link to comment Share on other sites More sharing options...
Recommended Posts