Reputation: 676
I am trying to compare 2 characters (or key codes) to check if the letter on the screen is the same as the pressed character.
Sadly, all the keyDown results are in upper-case, and I would like to know if there's a different way that gets input as lower-case instead of manually changing all the input.
Here's my code:
document.onkeydown = function keyDown(e) {
if (!e) e = window.event;
if (e.keyCode == currentCharacter.charCodeAt(0)) {
// Input matches the current character.
} else {
// Input does not match the current character.
}
}
In this example, e.keyCode always returns the keycode for an upper-case version of the character I pressed.
Upvotes: 0
Views: 995
Reputation: 4074
Using keyPress
event rather than keyDown
might be the answer.
Upvotes: 4
Reputation: 465
How about converting the keyCode to a char, and the lowercase it..
document.onkeydown = function keyDown(e) {
if (!e) e = window.event;
var keyPressed = String.fromCharCode(e.keyCode).toLowerCase()
if (keyPressed == 'a') {
// Input matches the current character.
} else {
// Input does not match the current character.
}
}
Upvotes: 0