Jump to content

Generic key input


codart
 Share

Recommended Posts

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 :D

Link to comment
Share on other sites

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));});
Link to comment
Share on other sites

 Share

  • Recently Browsing   0 members

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