Sign in to follow this  
codart

Generic key input

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

Share this post


Link to post
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));});

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  

  • Recently Browsing   0 members

    No registered users viewing this page.