Computer scienceProgramming languagesJavaScriptInteraction with a browserDOM Events

Keyboard events handling

E key

Report a typo

How do you correctly register a handler for an event that will happen at the moment the key "E" is pushed down on a keyboard?

The event handler must react at least for upper case letter "E" (usually Shift+E key combination), it may or may not react for lower case "e", when the key "E" is pressed down alone, without any other keys.

A)

document.addEventListener("keydown", function(event) {
  if (event.code == "Ekey") {
    console.log("The E key was pressed");
  }
});

B)

document.addEventListener("keydown", function(event) {
  if (event.code == "KeyE") {
    console.log("The E key was pressed");
  }
});

C)

document.addEventListener(function(event) {
  if (event.code == "KeyE") {
    console.log("The E key was pressed");
  }
});

D)

document.addEventListener("keydown", function(event) {
  if (event.code = "KeyE") {
    console.log("The E key was pressed");
  }
});

E)

document.addEventListener("keydown", function(event) {
  if (event.key == "E") {
    console.log("The E key was pressed");
  }
});
Select one or more options from the list
___

Create a free account to access the full topic