html,js获取扫码设备的输入内容

<script type="text/javascript"><!--

window.onload = function () {// 获取扫描的二维码内容

var code = "";

var lastTime, nextTime;

var lastCode, nextCode;

document.onkeypress = function (e) {

nextCode = e.which;

nextTime = new Date().getTime();

if (lastCode != null && lastTime != null && nextTime - lastTime <= 30) {// 扫码枪

code += String.fromCharCode(lastCode);

} else if (lastCode != null && lastTime != null && nextTime - lastTime > 100) { // 键盘

code = "";

}

lastCode = nextCode;

lastTime = nextTime;

}

this.doscan = function () {

this.onkeypress = function (e) {

if (e.which == 13) {

console.log(code);

console.log(code.length);

//后续代码

code = "";

}

}

}

this.doscan();

};

//--></script>

相关推荐
超哥--1 小时前
B站视频内容智能分析系统(九):React 前端与管理面板
前端·react.js·前端框架
Cutecat_4 小时前
视频字幕处理工具横向:提取模式 vs 编辑模式,该如何选择
android·前端·ios·语音识别
dsyyyyy11014 小时前
JavaScript变量
开发语言·javascript·ecmascript
qq_422152574 小时前
PDF 加水印工具怎么选?2026 年文档版权保护方案对比
前端·pdf·github
kyriewen4 小时前
手写 Promise.all、race、any:不到 30 行代码,解决并发异步的所有姿势
前端·javascript·面试
brucelee1865 小时前
OpenClaw 浏览器控制(Chrome MCP)完整教程
前端·chrome
ct9786 小时前
React 状态管理方案深度对比
开发语言·前端·react
胡志辉的博客6 小时前
深入浅出理解浏览器事件循环:从一道输出题讲到 Chrome 源码
前端·javascript·chrome·chromium·event loop
代码不加糖6 小时前
js中不会冒泡的事件有哪些?
前端·javascript·vue.js
懂懂tty6 小时前
Vue2与Vue3之间API差异
前端·javascript·vue.js