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. JAVAWEB dbutils执行sql命令并遍历结果集时不能查到内容的原因

    遍历结果集时只遍历bean对象才会只输出第一行那种内容(第一行是输出了UserEntity类实例化的对象),所以这里需要 re.getRepoTableName() 才能通过对象调用相对应的内容 这样 ...

  2. restful_framework之APIView

    一.安装djangorestframework 方式一:pip3 install djangorestframework 方式二:pycharm图形化界面安装 方式三:pycharm命令行下安装(装在 ...

  3. shell小记

    1.以#!/bin/bash 开头2.执行方式 bash sh ./ `script`3.变量: 系统变量  自定义变量  --->export 可导出为全局环境变量        set显示所 ...

  4. Kettle日常使用汇总整理

    Kettle日常使用汇总整理 Kettle源码下载地址: https://github.com/pentaho/pentaho-kettle Kettle软件下载地址: https://sourcef ...

  5. access和MySQL mssql

    Access.MSSQL.MYSQL数据库之间有什么区别?     Access数据库.MSSQL数据库.MYSQL数据库之间有什么区别?        不少企业和个人站长在网站制作时,会对数据库的概 ...

  6. Javascript开发者 常用知识

    Javascript是一种日益增长的语言,特别是现在ECMAScript规范按照每年的发布时间表发布.伴随着这门语言的规模化和快速发展,掌握JS(不仅仅是jQuery)的重要性,变得更加重要. 这不是 ...

  7. final文案+美工展示

    作业要求:https://edu.cnblogs.com/campus/nenu/SWE2017FALL/homework/1438 团队介绍:thunder 组成员及各位博客地址: 1.王航:htt ...

  8. 【探路者】final贡献分配

     [探路者]组成员及各位博客地址. 1蔺依铭:http://www.cnblogs.com/linym762/ 2张恩聚:http://www.cnblogs.com/zej87/ 3米赫:http: ...

  9. PC端上必应词典与金山词霸的测评分析

    1.  介绍 随着英语学习越来越普及,基本上现在每位大学生的电脑上都会有一款便捷的英语查词软件,这次我们团队选择测评的 是微软必应词典(3.5.0.4311)和金山词霸(2014.05.16.044) ...

  10. OO的第一次死亡

    久仰OO大名,总是想着提前做点准备,其实到头来还是什么准备都没有做,所以这学期就是从零开始的面向对象生活,也因此遇到了很多的问题. 第一次作业——多项式加减 第一次作业历来是较为简单的,但是对于面向对 ...