How to save what is typed on the screen in a variable

6

I have a web page, and sometimes users press some numbers.

However, I have a usb reader, which when I read a code it automatically shows me 7 numbers, as if I had typed them on the webpage.

How do I save these 7 numbers that are "typed" immediately on the webpage, without being input, with javascript?

    
asked by anonymous 19.12.2016 / 17:43

1 answer

6

One option is to put a listener in the document and store in an array the values pressed (or keyCodes) and do a continuous search on that array to see if what it has is what you are looking for - or even store only whatever you want based on keyCode:

document.addEventListener("keydown", keyDownPress, false);
var valoresDigitados = [];

function keyDownPress(e) {
  var keyCode = e.keyCode;
  valoresDigitados.push(String.fromCharCode(keyCode));
}

function verValores(){
 console.log(valoresDigitados.join(""));
}
<button onclick="verValores()">Ver valores</button>
    
19.12.2016 / 17:58