Sandah Aung Sandah Aung - 1 month ago 5
HTML Question

Text input to web page without text fields

Can a web page detect keyboard input without text input fields or with text input fields unfocused?

Answer

add an event listener to the document (run the example, and click on it to activate it, otherwise its document won't have the focus, this is intended to work on a standalone page rather than embedded frame)

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

function keydownCallback(e) {
  console.log(e.keyCode + ': "' + String.fromCharCode(e.keyCode) + '"');
}