Sandah Aung
Sandah Aung

Reputation: 6188

Catching keyboard events on window does not get special characters right

This script is supposed to catch key presses. But it is getting special characters wrongly received. For example, pressing \ results in Ü.

document.addEventListener('keydown', keydownCallback, false);

function keydownCallback(e) {
    var charCode = (typeof e.which == "number") ? e.which : e.keyCode;
    if (charCode) {
        console.log("Character typed: " + String.fromCharCode(charCode));
    }
}

Is there any way in which I can get the input characters correct?

Upvotes: 1

Views: 62

Answers (1)

JVDL
JVDL

Reputation: 1202

You could use KeyBoardEvent.key e.g. in your case e.key would be "\" when the backslash is pressed.

Upvotes: 1

Related Questions