将 keyDown 按下更改为小写

Changing keyDown presses to lower case

我正在尝试比较 2 个字符(或键码)以检查屏幕上的字母是否与按下的字符相同。

遗憾的是,所有 keyDown 结果都是大写的,我想知道是否有不同的方式将输入设为小写,而不是手动更改所有输入。

这是我的代码:

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.
  }
}

在这个例子中,e.keyCode总是returns我按下的字符的大写版本的键码。

According to this,

使用 keyPress 事件而不是 keyDown 可能是答案。

如何将 keyCode 转换为 char,并将其小写..

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.
  }
}