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

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
Sign in to follow this  

  • Recently Browsing   0 members

    No registered users viewing this page.