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. 开源项目CIIP(企业信息管理系统框架).2018.1.0910版更新介绍-上周工作总结

    又狂撸了一周的代码.简化了0904版本的多数操作. 上一次更新时,总共需要10步,这次简化成3步.嗯嗯,自我感觉不错. 重要的:在创建项目时,可以选择常用模块啦! 第一步:启动CIIP.Designe ...

  2. 在WebGL场景中进行棋盘操作的实验

    这篇文章讨论如何在基于Babylon.js的WebGL场景中,建立棋盘状的地块和多个可选择的棋子对象,在点选棋子时显示棋子的移动范围,并且在点击移动范围内的空白地块时向目标地块移动棋子.在这一过程中要 ...

  3. Unity学习笔记(4): 碰撞相关API

    Unity3D中的碰撞: 在unity中,有普通碰撞体Collider和触发器(Trigger)两种碰撞体,两个普通碰撞体会发生碰撞并产生力学现象,触发器则像是个无法触碰而又真实存在的东西,也会触发碰 ...

  4. 用UGUI制作可根据手指位置自动定位的隐形遥杆

    之前写过遥杆怎么做,这里依然用的是之前的方法,就不介绍了. 之前玩过<蜡烛人>,发现手游版的<蜡烛人>的遥杆是看不见的,手指直接在屏幕左边滑动人物就可以移动,可能是为了增强沉浸 ...

  5. visudo命令详解

    基础命令学习目录首页 原文链接:https://www.cnblogs.com/ImJerryChan/p/6667819.html 目录前言一.介绍二.配置文件简介三.实战配置 前言:    su ...

  6. mongodb基本使用(三)

    MongoDB 创建数据库 语法 MongoDB 创建数据库的语法格式如下: use DATABASE_NAME 如果数据库不存在,则创建数据库,否则切换到指定数据库. 如果你想查看所有数据库,可以使 ...

  7. Python之并发编程-多进程

    目录 一.multiprocessiong模块介绍 二.Process类的介绍 三.进一步介绍(守护进程.锁.队列.管道.事件等) 1.守护进程 2.锁(同步锁.互斥锁) 3.信号量(了解) 4.队列 ...

  8. oracle数据update后怎么恢复到以前的数据

    http://blog.csdn.net/itdada/article/details/52746392

  9. Notes of Daily Scrum Meeting(11.17)

    Notes of Daily Scrum Meeting(11.17) 今天是第四周的周一,也就是说距离最后发布也只剩下一周的时间,但我们的工程里面还有很多的问题没有解决,我关注过 其他一两个小组,他 ...

  10. Notes of Daily Scrum Meeting(11.3)

    Notes of Daily Scrum Meeting(11.3) 2014年11月3日  星期一  20:00—20:30 团队成员 今日团队任务 当日工作分配额 完成情况 陈少杰 阅读理解代码中 ...