From cbcf52e37fc5807829fe6de9cbbd30f52df205f5 Mon Sep 17 00:00:00 2001 From: anthonywj <yewenjie20@vip.qq.com> Date: Fri, 22 Apr 2022 09:04:04 +0800 Subject: [PATCH] =?UTF-8?q?=E6=89=AB=E7=A0=81=E7=9B=91=E5=90=AC=EF=BC=8C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/plugin/KeyScaner.js | 116 ++++++++++++++++++++++++++++ src/views/warehouse/IONewOrder.vue | 1 - src/views/warehouse/addHosOrder.vue | 36 +++++++++ 3 files changed, 152 insertions(+), 1 deletion(-) create mode 100644 src/plugin/KeyScaner.js diff --git a/src/plugin/KeyScaner.js b/src/plugin/KeyScaner.js new file mode 100644 index 0000000..ee42882 --- /dev/null +++ b/src/plugin/KeyScaner.js @@ -0,0 +1,116 @@ +"use strict"; +/** + * 浏览器按键处理 + * howay + * 20200421 + * 1.0 + */ + const KeyScaner = /** @class */ (function () { + function KeyScaner(_dom) { + this.keybufs = []; + this.altBuf = []; + this.isShift = false; + this.lastTime = 0; + /** + * 在收集到文本输入时触发 + * @param text 收集到的连续文本 + */ + this.onInput = function (text) { }; + this.dom = _dom; + _dom.addEventListener('keydown', this.onKeyDown.bind(this)); + _dom.addEventListener('keyup', this.onKeyUp.bind(this)); + this.interval = setInterval(this.onTick.bind(this), 100); + } + KeyScaner.prototype.appendChar = function (c) { + this.keybufs.push(c); + }; + KeyScaner.prototype.onKeyDown = function (ev) { + this.lastTime = new Date().getTime(); + if (ev.key == "Alt") { + if (ev.shiftKey || ev.ctrlKey || ev.metaKey) { + return; + } + } + else if (ev.key == "Shift") { + if (ev.altKey || ev.ctrlKey || ev.metaKey) { + return; + } + this.isShift = true; + } + else if (ev.key == "Enter") { + if (ev.shiftKey || ev.altKey || ev.ctrlKey || ev.metaKey) { + return; + } + this.appendChar("\r"); + this.completeInput(); //立即完成输入 + } + else { + if (ev.key.length == 1) { + var charCode = ev.key.charCodeAt(0); + if (charCode >= 48 && charCode <= 57) { + if (ev.altKey) { + this.altBuf.push(ev.key); + } + else { + this.appendChar(ev.key); + } + } + else if ((charCode >= 65 && charCode <= 90) || (charCode >= 97 && charCode <= 122)) { + //忽略大小写锁定键,直接通过SHIFT键判断大小写 + if (ev.shiftKey) { + this.appendChar(ev.key.toUpperCase()); + } + else { + this.appendChar(ev.key.toLowerCase()); + } + } + else { + this.appendChar(ev.key); + } + } + //忽略其他控制键 + } + }; + KeyScaner.prototype.onKeyUp = function (ev) { + this.lastTime = new Date().getTime(); + if (ev.key == "Alt") { + if (this.altBuf && this.altBuf.length > 0) { + var str = this.altBuf.join(""); + var num = parseInt(str); + var c = String.fromCharCode(num); + this.appendChar(c); + this.altBuf = []; + } + } + else if (ev.key == "Shift") { + this.isShift = false; + } + }; + KeyScaner.prototype.completeInput = function () { + var text = this.keybufs.join(""); + this.keybufs = []; + if (this.onInput) { + this.onInput.bind(this)(text); + } + }; + KeyScaner.prototype.onTick = function () { + if (this.keybufs && this.keybufs.length > 0 && (!this.altBuf || this.altBuf.length == 0) && new Date().getTime() - this.lastTime > 500) { + //有缓存的字符,且没有缓存的alt字符 且 与上次按键时间超过500毫秒。 收集输入 + this.completeInput(); + } + }; + /** + * 释放资源 + */ + KeyScaner.prototype.dispose = function () { + this.dom.removeEventListener('keydown', this.onKeyDown); + this.dom.removeEventListener('keyup', this.onKeyUp); + clearInterval(this.interval); + }; + return KeyScaner; +}()); + +export default { + KeyScaner +} + diff --git a/src/views/warehouse/IONewOrder.vue b/src/views/warehouse/IONewOrder.vue index 2801688..12e3dd9 100644 --- a/src/views/warehouse/IONewOrder.vue +++ b/src/views/warehouse/IONewOrder.vue @@ -688,7 +688,6 @@ export default { this.filterQuery = Object.assign(this.filterQuery, query); this.filterQuery.limit = parseInt(this.filterQuery.limit); - }, }; </script> diff --git a/src/views/warehouse/addHosOrder.vue b/src/views/warehouse/addHosOrder.vue index f28907e..b9f665e 100644 --- a/src/views/warehouse/addHosOrder.vue +++ b/src/views/warehouse/addHosOrder.vue @@ -99,6 +99,7 @@ </el-row> + <el-row :gutter="20"> @@ -192,6 +193,7 @@ @keypress.enter.native="enterKey($event)" v-model="formData.code" ref='inputRef' + id="inputer" ></el-input> </el-form-item> </el-col> @@ -332,6 +334,8 @@ import {filterAll, filterAllByLoc, filterAllByUser} from "@/api/basic/invWarehou import {getLocalBusType, getLocalJoinBusType, getLocalJoinByUser} from "../../api/basic/busLocalType"; import DialogSelectUnit from "./DialogSelectUnit"; import selectRlDialog from "./DialogSelectRl"; +import A from "../../plugin/KeyScaner" + export default { name: "idQuery", @@ -748,6 +752,22 @@ export default { this.loading = false; }); }, + listenerFunction(e) { + document.addEventListener('DOMContentLoaded', function () { + + console.log("-----jianting---"); + document.removeEventListener('DOMContentLoaded', arguments.callee, false); + var inputer = document.getElementById("inputer"); + window.sc = new KeyScaner(inputer);//传入要监听的DOM节点 + sc.onInput = function (text) { + //onInput事件在检测到回车键按下或在连续输入后超过500ms没有继续输入时触发 + inputer.innerText += (text + "\n"); + console.log("--------" + text); + // this.formData.code = + }; + inputer.focus();//别忘了给要监听的节点放置焦点,如果是div一类默认不具备焦点的节点需要给它加上“tabindex”属性。不建议传入document、Body等全局节点,可能会影响其它输入控件的流畅性。 + }, false); + }, findStorageMethod(query) { if (this.formData.locStorageCode == null) @@ -872,12 +892,24 @@ export default { }, }, + + filters: {}, mounted() { document.body.ondrop = function (event) { event.preventDefault(); event.stopPropagation(); }; + var inputer = document.getElementById("inputer"); + window.sc = new A.KeyScaner(inputer);//传入要监听的DOM节点 + sc.onInput = function (text) { + //onInput事件在检测到回车键按下或在连续输入后超过500ms没有继续输入时触发 + inputer.innerText += (text + "\n"); + console.log("--------" + text); + // this.formData.code = + }; + inputer.focus();//别忘了给要监听的节点放置焦点,如果是div一类默认不具备焦点的节点需要给它加上“tabindex”属性。不建议传入document、Body等全局节点,可能会影响其它输入控件的流畅性。 + }, created() { this.formData.code = ''; @@ -918,9 +950,13 @@ export default { } } + this.listenerFunction(); + }, }; </script> + + <style> .ao-text {