Ben
Ben

Reputation: 1914

how to make a keycode work for cyrillic

Below code work only for latin n, but how to make it work for cyrillic н

if(event.keyCode === 78) {
console.log('okay');
}

And is weird that this code works if i change the keyboard layout different from US and click on latin N

Upvotes: 1

Views: 3832

Answers (2)

Tim Down
Tim Down

Reputation: 324627

I'm not quite sure what you're asking, but I'm going to assume you want to get the character associated with typing in a cyrillic character on a keyboard. If so, I imagine you're using the wrong event (keydown or keyup) when the correct event is keypress. keydown and keyup tell you about the physical key pressed while keypress tells you about the character that was typed. Here's a simple cross-browser example of how to get the typed character:

document.onkeypress = function(e) {
    e = e || window.event;
    var charCode = (typeof e.which == "number") ? e.which : e.keyCode;
    var typedChar = String.fromCharCode(charCode);
    alert("Typed character: " + typedChar);
};

Here's the definitive reference for JavaScript keyboard events: http://unixpapa.com/js/key.html

Upvotes: 4

Li0liQ
Li0liQ

Reputation: 11264

Cyrillic н is not a part of first 127 characters of UTF8 therefore your code does not work. You could try using appropriate code for н, that is 1085, if it makes any sense:

if(event.keyCode === 1085) {
    console.log('okay');
}

As a side note, there is no one-to-one relationship between cyrillic and latin characters - I would suggest thinking twice before trying to invent one.

Upvotes: 0

Related Questions