0

is there a way to get keyboard input in plain javascript? I've searched it up but everything was with HTML. I've tried this. but it says document is not defined

document.addEventListener('keydown', function(event) { console.log(event.keyCode)
  if (event.keyCode == 37) {
    console.log('Left was pressed');
  } else if (event.keyCode == 39) {
    console.log('Right was pressed');
  }
});
mplungjan
  • 169,008
  • 28
  • 173
  • 236
  • 2
    If you are not in a browser, you do not have a document – mplungjan Nov 02 '21 at 11:46
  • If you are running node from the command prompt, then this is the answer: https://stackoverflow.com/questions/5006821/nodejs-how-to-read-keystrokes-from-stdin – mplungjan Nov 02 '21 at 11:49
  • Also if it is in a browser, then you have readable shortcuts : https://stackoverflow.com/a/44213036/295783 – mplungjan Nov 02 '21 at 11:51

0 Answers0