JS获取扫描枪扫描的条形码(VUE)
扫码枪是模拟键盘输入的在vue中,在created中调用created() {var _this=thisvar codeString = "";//定时器每隔200ms 清空codeString//扫码枪读取的速度比手动输入的速度要快很多,这也可以作为区分扫码还是手动输入的条件var scanTimeer = setInterval(function() {var now = new Date(
·
扫码枪是模拟键盘输入的
created() {
var _this=this
var codeString = "";
//定时器每隔200ms 清空codeString
//扫码枪读取的速度比手动输入的速度要快很多,这也可以作为区分扫码还是手动输入的条件
var scanTimeer = setInterval(function() {
var now = new Date().getTime();
if (now - lastTime > 200) {
codeString = "";
}
}, 200);
var lastTime;
var caseFormat = false;
document.onkeydown = function(e) {
var nextTime = new Date().getTime();
var code = e.which;
//shift键
if (code == 16) {
caseFormat = true;
} else {
if (caseFormat) {
if (code >= 65 && code <= 90) {
//转小写
code = code + 32;
} else if (code >= 97 && code <= 122) {
//转大写
code = code - 32;
}
caseFormat = false;
}
var char = String.fromCharCode(code);
if (codeString == "") {
codeString += char;
} else if (nextTime - lastTime <= 30) {
codeString += char;
}
}
lastTime = nextTime;
};
window.onkeydown=(function(e) {
var nextTime1 = new Date().getTime();
var lastTime1;
if (e.which ==13){
_this.onScanning(codeString.substring(0, codeString.length - 1));
codeString = "";
}
})
},
},
onScanning(code){
console.log(code)//获取到的条形码
},
更多推荐
已为社区贡献3条内容
所有评论(0)