codart Posted June 18, 2014 Share Posted June 18, 2014 Hello everyone, let me show you my case, i'm making a typing game in which player should be able to input keys in range of [a-z]. My problem is i do not find any thing like:game.input.onKeyDown(keyHandle);function keyHandle(keyCode) { if(keyCode===65) { console.log("A"); }}i just dont want to add handle for every key, it's seem to be overkill to me :<THank you for reading Link to comment Share on other sites More sharing options...
codart Posted June 18, 2014 Author Share Posted June 18, 2014 bump! anybody here? Link to comment Share on other sites More sharing options...
lewster32 Posted June 18, 2014 Share Posted June 18, 2014 Keyboard.onDownCallback combined with game.input.keyboard.event.keyCode will do this: game.input.keyboard.onDownCallback = function() { console.log(game.input.keyboard.event.keyCode); }; Link to comment Share on other sites More sharing options...
codart Posted June 18, 2014 Author Share Posted June 18, 2014 lewster32, you saved my day. It's really, realy help full lewster32 1 Link to comment Share on other sites More sharing options...
lewster32 Posted June 18, 2014 Share Posted June 18, 2014 You're very welcome Just as a reminder, you're not forced to use Phaser-specific methods to capture inputs or indeed do pretty much anything; standard JavaScript ways of doing this will fit into Phase quite comfortably. codart 1 Link to comment Share on other sites More sharing options...
foolmoron Posted June 18, 2014 Share Posted June 18, 2014 Keep in mind that in Firefox, you don't get the pressed key with event.keyCode, but with event.which. So if you want portability, you need to do:var key = event.keyCode || event.which;Further, if you're trying to get which characters are actually typed by the player, with the right capitalization, then you should use the keypress event instead of the keydown event. Keypress doesn't fire when non-ASCII keys like Shift or F2 are pressed, only when an actual character is input. It does fire on enter/return, though (keycode 13). For example://When pressing A, this event logs 'a'//When pressing Shift + A, this event logs 'A'window.addEventListener('keypress', function(event) { var key = event.keyCode || event.which; console.log("keypress = " + String.fromCharCode(key));});//When pressing A, this event logs 'A'//When pressing Shift + A, this event logs '' and 'A'window.addEventListener('keydown', function(event) { var key = event.keyCode || event.which; console.log("keydown = " + String.fromCharCode(key));}); codart and lewster32 2 Link to comment Share on other sites More sharing options...
Recommended Posts