{"id":227710,"date":"2021-10-10T08:44:53","date_gmt":"2021-10-10T00:44:53","guid":{"rendered":"https:\/\/lrxjmw.cn\/?p=227710"},"modified":"2021-09-30T09:45:37","modified_gmt":"2021-09-30T01:45:37","slug":"js-usb-input","status":"publish","type":"post","link":"https:\/\/lrxjmw.cn\/js-usb-input.html","title":{"rendered":"\u5982\u4f55\u4f7f\u7528js\u83b7\u53d6USB\u626b\u7801\u67aa\u6570\u636e"},"content":{"rendered":"\n\n\n
\u5bfc\u8bfb<\/td>\n\u8fd9\u7bc7\u6587\u7ae0\u4e3b\u8981\u4e3a\u5927\u5bb6\u8be6\u7ec6\u4ecb\u7ecd\u4e86js\u83b7\u53d6USB\u626b\u7801\u67aa\u6570\u636e\u7684\u65b9\u6cd5\uff0c\u6587\u4e2d\u793a\u4f8b\u4ee3\u7801\u4ecb\u7ecd\u7684\u975e\u5e38\u8be6\u7ec6\uff0c\u5177\u6709\u4e00\u5b9a\u7684\u53c2\u8003\u4ef7\u503c\uff0c\u611f\u5174\u8da3\u7684\u5c0f\u4f19\u4f34\u4eec\u53ef\u4ee5\u53c2\u8003\u4e00\u4e0b<\/strong><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n

\"\"<\/p>\n

\u672c\u6587\u5b9e\u4f8b\u4e3a\u5927\u5bb6\u5206\u4eab\u4e86js\u83b7\u53d6USB\u626b\u7801\u67aa\u6570\u636e\u7684\u5177\u4f53\u8fc7\u7a0b\uff0c\u4f9b\u5927\u5bb6\u53c2\u8003\uff0c\u5177\u4f53\u5185\u5bb9\u5982\u4e0b<\/p>\n

\u5e9f\u8bdd\u4e0d\u591a\u8bf4\uff0c\u76f4\u63a5\u4e0a\u4ee3\u7801\uff0c\u8fd9\u4e2a\u65b9\u6cd5\u907f\u514d\u4e86 \u9996\u5b57\u7f3a\u5931\uff0c\u5b57\u7b26\u4e32\u4e71\u7801\u7b49\u7b49\u95ee\u9898\uff0c\u7279\u522b\u597d\u7528\uff0c\u8bb0\u5f55\u4e00\u4e0b<\/p>\n

\u4e2d\u95f4\u7684Ajax\u53ef\u76f4\u63a5\u8c03\u7528\u767b\u5f55\u529f\u80fd\uff0c\u5b9e\u73b0\u81ea\u52a8\u767b\u5f55<\/p>\n

\u8bf4\u660e\uff1a\u8f93\u5165\u6846\u9700\u8981\u83b7\u5f97\u7126\u70b9\uff0c\u8fd9\u4e2a\u662f\u5fc5\u987b\u7684\uff0c\u5176\u6b21\u4e3a\u4e86\u9632\u6b62\u56de\u8f66\u89e6\u53d1\u624b\u52a8\u767b\u5f55\uff0c\u9700\u8981\u6dfb\u52a0<\/p>\n

\u03bfnkeypress=\"if(event.keyCode==13) return false;\"<\/pre>\n

\u8fd9\u4e2a\u64cd\u4f5c\uff0c\u8fd9\u4e2a\u5728\u56de\u8f66\u7684\u65f6\u5019\uff0c\u8f93\u5165\u6846\u7126\u70b9\u4e0d\u4f1a\u4e22\u5931\uff0c\u9664\u975e\u624b\u52a8\u5207\u6362\u7126\u70b9\uff0c\u6216\u8005\u8c03\u7528focus();\u65b9\u6cd5<\/p>\n

\r\n<div class=\"form-group\">\r\n <label for=\"inputUsernameEmail\">\u8d26\u53f7<\/label>\r\n <input type=\"text\" placeholder=\"\u8bf7\u8f93\u5165\u8d26\u53f7\" name=\"username\" id=\"loginname\" class=\"form-control\" autofocus onkeypress=\"if(event.keyCode==13) return false;\">\r\n<\/div>\r\n<\/pre>\n

\u4ee3\u7801\uff1a<\/p>\n

\r\n<script>\r\n  window.onload = (e)=> {\r\n   this.start = new Date().getTime()\r\n   let code = ''\r\n   let lastTime, nextTime\r\n   let lastCode, nextCode\r\n   let that = this\r\n   window.document.onkeypress = function (e) {\r\n    if (window.event) { \/\/ IE\r\n     nextCode = e.keyCode\r\n    } else if (e.which) { \/\/ Netscape\/Firefox\/Opera\r\n     nextCode = e.which\r\n    }\r\n    console.log('nextCode', nextCode)\r\n    if (e.which === 13 || window.event === 13) {\r\n     var deviceCode = code;\r\n     console.log(code)\r\n     console.log('\u626b\u7801\u7ed3\u675f')\r\n     console.timeEnd()\r\n     code = ''\r\n     lastCode = ''\r\n     lastTime = ''\r\n     $.ajax({\r\n      cache: true,\r\n      type: \"POST\",\r\n      url: rootPath + \"\/admin\/login\",\r\n      data: {code: deviceCode, type: 1},\r\n      async: false,\r\n      error: function (request) {\r\n       $(\"#loginname\").val(\"\");\r\n       $.modal.alertError(\"\u7cfb\u7edf\u9519\u8bef\");\r\n      },\r\n      success: function (data) {\r\n       $(\"#loginname\").val(\"\");\r\n       if (data.code == 200) {\r\n        location.href = rootPath + '\/admin\/index';\r\n       } else {\r\n        $.modal.alertError(data.msg);\r\n       }\r\n      }\r\n     });\r\n    }\r\n    nextTime = new Date().getTime()\r\n    if (!lastTime && !lastCode) {\r\n     console.log('\u626b\u7801\u5f00\u59cb\u3002\u3002\u3002')\r\n     code += e.key\r\n    }\r\n    if (lastCode && lastTime && nextTime - lastTime > 500) { \/\/ \u5f53\u626b\u7801\u524d\u6709keypress\u4e8b\u4ef6\u65f6,\u9632\u6b62\u9996\u5b57\u7f3a\u5931\r\n     console.log('\u9632\u6b62\u9996\u5b57\u7f3a\u5931\u3002\u3002\u3002')\r\n     code = e.key\r\n    } else if (lastCode && lastTime) {\r\n     console.log('\u626b\u7801\u4e2d\u3002\u3002\u3002')\r\n     code += e.key\r\n    }\r\n    lastCode = nextCode\r\n    lastTime = nextTime\r\n   }\r\n  }\r\n<\/script>\r\n<\/pre>\n

\u4ee5\u4e0a\u5c31\u662f\u672c\u6587\u7684\u5168\u90e8\u5185\u5bb9\uff0c\u5e0c\u671b\u5bf9\u5927\u5bb6\u7684\u5b66\u4e60\u6709\u6240\u5e2e\u52a9<\/p>\n","protected":false},"excerpt":{"rendered":"

\u672c\u6587\u5b9e\u4f8b\u4e3a\u5927\u5bb6\u5206\u4eab\u4e86js\u83b7\u53d6USB\u626b\u7801\u67aa\u6570\u636e\u7684\u5177\u4f53\u8fc7\u7a0b\uff0c\u4f9b\u5927\u5bb6\u53c2\u8003\uff0c\u5177\u4f53\u5185\u5bb9\u5982\u4e0b \u5e9f\u8bdd\u4e0d\u591a\u8bf4\uff0c\u76f4\u63a5\u4e0a\u4ee3\u7801\uff0c\u8fd9\u4e2a […]<\/p>\n","protected":false},"author":362,"featured_media":227714,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[55],"tags":[],"class_list":["post-227710","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-thread"],"acf":[],"_links":{"self":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts\/227710","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/users\/362"}],"replies":[{"embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/comments?post=227710"}],"version-history":[{"count":4,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts\/227710\/revisions"}],"predecessor-version":[{"id":227716,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts\/227710\/revisions\/227716"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/media\/227714"}],"wp:attachment":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/media?parent=227710"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/categories?post=227710"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/tags?post=227710"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}