js & 快捷键 & vue bind bug

how to prevent addEventListener bind many times

solution

dataset & once flag

// flag

// shortcut keys
keyboardShortcutKeys() {
let that = this;
let body = document.querySelector(`body`);
let bindFlag = body.dataset.bindFlag;
// console.log(`bindFlag =`, bindFlag);
if (bindFlag === "true") {
console.log(`only need binding once!`);
} else {
body.dataset.bindFlag = "true";
// bind once
body.addEventListener("keyup", function(e) {
let key = e.which || e.keyCode;
if(e.which === 17) {
// init
this.isCtrlPressed = false;
}
});
body.addEventListener("keydown", function(e) {
let key = e.which || e.keyCode;
if(e.which === 17) {
this.isCtrlPressed = true;
}
let isCtrl = this.isCtrlPressed;
if(e.which === 83 && isCtrl) {
console.log(`you pressed Ctrl + S`);
if (that.onClickButton) {
that.onClickButton(`save`);
} else {
console.log(`typeof(that.onClickButton) =`, typeof(that.onClickButton));
console.log(`typeof(that.xyz) =`, typeof(that.xyz));
}
}
if(e.which === 68 && isCtrl) {
console.log(`you pressed Ctrl + D`);
if (that.clickGetNewsInfos) {
that.clickGetNewsInfos(`prev`);
}
}
if(e.which === 70 && isCtrl) {
console.log(`you pressed Ctrl + F`);
if (that.clickGetNewsInfos) {
that.clickGetNewsInfos(`next`);
}
}
if(e.which === 88 && isCtrl) {
console.log(`you pressed Ctrl + X`);
if (that.clickShowResureModal) {
that.clickShowResureModal(`passed`);
}
}
});
}
},

// shortcut keys
keyboardShortcutKeys() {
let that = this;
let body = document.querySelector(`body`);
let bindFlag = body.dataset.bindFlag;
// console.log(`bindFlag =`, bindFlag);
if (bindFlag === "true") {
console.log(`only need binding once!`);
} else {
body.dataset.bindFlag = "true";
// bind once
body.addEventListener("keyup", function(e) {
let key = e.which || e.keyCode;
if(e.which === 17) {
// init
this.isCtrlPressed = false;
}
});
body.addEventListener("keydown", function(e) {
let key = e.which || e.keyCode;
if(e.which === 17) {
this.isCtrlPressed = true;
}
let isCtrl = this.isCtrlPressed;
if(e.which === 83 && isCtrl) {
console.log(`you pressed Ctrl + S`);
if (that.onClickButton) {
that.onClickButton(`save`);
}
}
if(e.which === 68 && isCtrl) {
console.log(`you pressed Ctrl + D`);
if (that.clickGetNewsInfos) {
if (!that.isFirstNews) {
that.clickGetNewsInfos(`prev`);
} else {
// btn bug
console.log(`prev btn bug!`);
}
}
}
if(e.which === 70 && isCtrl) {
console.log(`you pressed Ctrl + F`);
if (that.clickGetNewsInfos) {
if (!that.isLastNews) {
that.clickGetNewsInfos(`next`);
} else {
// btn bug
console.log(`next btn bug!`);
}
}
}
if(e.which === 88 && isCtrl) {
console.log(`you pressed Ctrl + X`);
if (that.clickShowResureModal) {
that.clickShowResureModal(`passed`);
}
}
});
}
},

bug

快捷键,与浏览器自带的冲突了

  1. 保存CTRL + Alt + S;上一条CTRL + Alt + D;下一条CTRL + Alt + F;处理通过CTRL + Alt + X , windows 可以正常使用

  1. 保存CTRL+S;上一条CTRL+D;下一条CTRL+F;处理通过CTRL+X, MacOS 可以正常使用

https://codepen.io/webgeeker/full/YBPBOV

js & 快捷键 & vue bind bug的更多相关文章

  1. vue & lifecycle methods & this bug & ES6 Arrow function & this bind bug

    vue & lifecycle methods & this bug ES6 Arrow function & this bind bug bad fetchTableData ...

  2. iview render bug & vue namespace bug

    iview render bug https://codepen.io/xgqfrms/pen/gyGjKP https://codepen.io/xgqfrms/full/gyGjKP bug &l ...

  3. vue & less bug

    vue & less bug bezierEasingMixin(); ^ Inline JavaScript is not enabled. Is it set in your option ...

  4. vue-cli & plugin:vue/strongly-recommended bug

    vue-cli & plugin:vue/strongly-recommended bug ESLint plugin:vue/strongly-recommended module.expo ...

  5. Vue.js起手式+Vue小作品实战

    本文是小羊根据Vue.js文档进行解读的第一篇文章,主要内容涵盖Vue.js的基础部分的知识的,文章顺序基本按照官方文档的顺序,每个知识点现附上代码,然后根据代码给予个人的一些理解,最后还放上在线编辑 ...

  6. vue.runtime.esm.js:593 [Vue warn]: Invalid prop: custom validator check failed for prop "value".报错解决

    在uni中使用 picker组件,一直报错 vue.runtime.esm.js:593 [Vue warn]: Invalid prop: custom validator check failed ...

  7. js & 快捷键

    js & 快捷键 demo js-keyboard-shortcuts.html https://codepen.io/webgeeker/pen/MLYrNq let isCtrl = fa ...

  8. js原生函数bind

    /*在javascript中,函数总是在一个特殊的上下文执行(称为执行上下文),如果你将一个对象的函数赋值给另外一个变量的话,这个函数的执行上下文就变为这个变量的上下文了.下面的一个例子能很好的说明这 ...

  9. 如何在其他js 引入main.js 中 vue 的实例?

    1.原因解析 经测试发现,代码先执行了 index.js >>  main.js >> Home.vue scr/api/index.js src/main.js src/co ...

随机推荐

  1. vue复习(二)

    一.组件介绍 每一个组件都是一个vue实例 每个组件均具有自身的模板template,根组件的模板就是挂载点 每个组件模板只能拥有一个根标签 子组件的数据具有作用域,以达到组件的复用 二.局部组件 & ...

  2. 快速搭建属于自己的mongodb数据库

    前言 MongoDB 是一个基于分布式文件存储的数据库.由C++语言编写,支持Windows,Linux,OSX,Solaris等平台,默认端口为27017,是一个效率非常高的nosql数据库. 我的 ...

  3. svn检出项目后,serverlet包 报错

    因为缺少一个包    servlet-api.jar   没引.

  4. 微信小程序中的分享事件

    小程序的分享 onShareAppMessage(options)   在页面的js文件中定义了 onShareAppMessage 函数时,页面可以表示改页面可以转发.可以在函数中设置页面转发的信息 ...

  5. zabbix监控DELL服务器硬件状态

    zabbix监控DELL服务器硬件状态 登录dell服务的管理页面 默认用户名:root 密码:calvin 服务器开放snmp信息,开启完应用 Zabbix服务器导入dell监控硬件模板 验证 sn ...

  6. Unity3D — — Inspector面板编辑

    转载官方文档,暂未深入研究 PropertyDrawer

  7. 来源自rnnoise,但非rnn

    很快又一年过去了. 自学音频算法也近一年了. 不记得有多少个日日夜夜, 半夜醒来,就为验证算法思路. 一次又一次地改进和突破. 傻逼样的坚持,必然得到牛逼样的结果. 这一年,主要扎音频算法上. 经常有 ...

  8. MAVEN项目导入src/test/java项目报错

    转载博客:https://blog.csdn.net/gengjianchun/article/details/78679036    https://blog.csdn.net/jsloveyou/ ...

  9. 3星|《实战复盘第四季·商业巨头们的变革之道》:GE、TCL、力拓集团、英美资源集团等企业总裁的变更经验

    实战复盘第四季·商业巨头们的变革之道(<哈佛商业评论>增刊) 本期是<哈佛商业评论>“实战复盘”栏目的10篇文章,讲的是GE.TCL.力拓集团.英美资源集团等企业如何熬过变革期 ...

  10. centos7 上安装mysql5.7后登录报错ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: Yes 或者No)

    原文转载自以下链接:https://blog.csdn.net/keepd/article/details/77151006 安装完mysql后会有个临时密码去日志查看,但是查看登录修改密后还是不行 ...