0

I everyone, I must convert a character to keyCode... I must not convert to charCode...

For example if I have a "." (a dot) I need to obtain "190" as result.. but If I do

".".charCodeAt(0)

I obtain "46"... "46" is not the result I want... I have also tried to do

String.fromCharCode(190);

In this case I do not obtain "." but I obtain "3/4".

How can I convert the character to obtain "190"?

UPDATE I must compare the input from the keyboard with a value in a "string"... I need to do something like this

window.addEventListener('keydown', function(e){
    if(e.keyCode == ".") <-- I need to get the keyCode of the "."
    {
        //Do something
    }
});

That is an example.... I do not know which is the char I could need to compare... It could be a ".", or a ",", o something else...

Thank you all.

Simone
  • 149
  • 2
  • 9
  • 1
    I updated my question... however if I press the "." on the keyboard I can see the "." has the code 190... see the answer of @Torean – Simone May 05 '15 at 09:58

2 Answers2

0

I was thinking and you reminded me of something simelar I needed to do. what you can do is maybe use a eventListener and log the code to the console. for example:

window.addEventListener('keydown', function(e){
    console.log(e.keyCode);
});

This will just log out the keycode of every pressed key on the keyboard

here is the link to the JS fiddle: https://jsfiddle.net/ToreanJoel/09798khL/

TrojanMorse
  • 642
  • 1
  • 8
  • 16
0

While I do not have a solution for converting special characters to keyCodes (also see my answer here: https://stackoverflow.com/a/49897083/9219743 ), here another solution for your problem - just use:

if(e.key == '.') {//do something}

So compare to e.key (which is the actual character) instead of e.keyCode.

klues
  • 847
  • 12
  • 21