本文摘自PHP中文网,作者coldplay.xixi,侵删。

1、扫码枪相当于键盘输入设备,输入一连串数字后加一个enter键。但在实际开发中需要区分是扫描枪输入还是键盘用户输入,区别在于扫码枪输入很快。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 | let code = '' ;
let lastTime, nextTime;
let lastCode, nextCode;
window.document.onkeypress = (e) => {
if (window.event) {
nextCode = e.keyCode;
} else if (e.which) {
nextCode = e.which;
}
if (nextCode === 13) {
if (code.length < 3) return ;
console.log(code);
code = '' ;
lastCode = '' ;
lastTime = '' ;
return ;
}
nextTime = new Date().getTime();
if (!lastTime && !lastCode) {
code += e.key;
}
if (lastCode && lastTime && nextTime - lastTime > 30) {
code = e.key;
} else if (lastCode && lastTime) {
code += e.key;
}
lastCode = nextCode;
lastTime = nextTime;
}
|
PS:下面看下js获取USB扫码枪数据的代码
前言
找了很多相关的教程不太好用,汲取各家之长总结精简了一下
阅读剩余部分
相关阅读 >>
javascript如何判断节点是否存在
js的图片处理与合成详解
17个你不知道的实用javascript技巧!
javascript数组删除的方法有哪些
javascript中substr和substring的区别是什么
javascript中字符串替换函数是什么
javascript中map方法怎么用
javascript深入探索 websocket和http/2与sse +如何选择正确的路径!
dom节点和元素之间有什么区别
多行文本进行截断的奇淫巧技
更多相关阅读请进入《javascript》频道 >>
人民邮电出版社
本书对 Vue.js 3 技术细节的分析非常可靠,对于需要深入理解 Vue.js 3 的用户会有很大的帮助。——尤雨溪,Vue.js作者
转载请注明出处:木庄网络博客 » 一起看看js获取扫码枪输入数据的方法