About js get input scan code gun acquisition mode, before a lot of online search, are much the same, he has said time interval of the input scan code gun no more than 30 milliseconds. But in fact I took a few computers test result, and some even more than the time interval of 100 milliseconds, to judge by the time interval is entered manually enter or scan code gun, the feeling is not very good, so I changed a species logic to achieve access to the input scan code gun, as follows:
scanCode() { let code = '' let lastTime, nextTime let lastCode, nextCode let that = this window.document.onkeypress = function(e) { if (window.event) { // IE nextCode = e.keyCode } else if (e.which) { // Netscape/Firefox/Opera nextCode = e.which } if (e.which === 13) { if (code.length < 3) return //Manually entering the code length does not allow more than 2, so there will only scan code gun has the console.log (code) the console.log ( 'end of the scan code' ) // that.distinguishCode (code) // acquired SUMMARY gun sweep code input, do other operation code = '' lastCode = '' lastTime = '' return } NextTime = new new a Date (). the getTime () IF (! lastTime &&! lastCode) { the console.log ( 'sweep code start ... ' ) code + = e.key } IF (lastCode && && NextTime lastTime - lastTime>500) { //When a keypress event before the scan code to prevent the first word deletion console.log ( 'prevent the first word deletion ...' ) code = e.key } the else IF (lastCode && lastTime) { the console.log ( 'scan code. .. ' ) code + = e.key } lastCode = nextCode lastTime = NextTime } }