JavaScript's String.fromCharCode() not returning keydown characters accurately, is there another function?

I have a keydown event and I need to convert the event's event.keyCode to its character equivalent. I'm using String.fromCharCode(), but it returns a '3' when I press a '#'.

Is there another function I can use that would convert event.keyCode values to their character equivalents accurately? Thanks in advance.

Here is the code snippet:

<?php
require_once $_SERVER['DOCUMENT_ROOT'] . "/_includes/init.php";
require_once ROOT . "/_includes/_db/DB.php";
require_once ROOT . "/_includes/dao/User.php";
?>
<!DOCTYPE html>
<html>
<head>
<title></title>
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.11.2/jquery.min.js"></script>
<script src="../../_scripts/utils.js"></script>
<script>
    $(function() {
        $("#txtText").keydown(function(event) {
            var sInput = String.fromCharCode(event.keyCode);
            if ("ABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890".indexOf(sInput) !== -1) {
                console.log(sInput);
            } else {
                return false;
            }
        });
    });

</script>
<style>
</style>
</head>
<body>    
    <input id="txtText" />
</body>
</html>

Open in new window

elepilAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

RobOwner (Aidellio)Commented:
You would have to do this mapping yourself but event.shiftKey you'll notice is true so it detects that you've pressed the "3" key with the "shift" key held down.

If you use the keypress event however you'll see that jQuery normalises the "which" property of the event such that it will show the correct character
RobOwner (Aidellio)Commented:
in other words:

$("#txtText").keypress(function(event) {
          console.dir(event);
            var sInput = String.fromCharCode(event.which);
            if ("ABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890".indexOf(sInput) !== -1) {
                console.log(sInput);
            } else {
                return false;
            }
        });

Open in new window

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
elepilAuthor Commented:
Thanks, Rob!
elepilAuthor Commented:
Rob, I thought I'd course this through you since I've pretty much implemented what you told me. I'm having a couple of issues. You can see it here: http://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/Q_28681770.html.

Thanks.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
JavaScript

From novice to tech pro — start learning today.