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. CF97C Winning Strategy

    CF97C Winning Strategy 洛咕(题意应该快传上去了) 这题好玄学鸭...都不知道为啥是对的 设\(f[i][j]\)表示打了i轮比赛,有j个参加了一次的人,直接枚举有几个参加了转移 ...

  2. 初窥Linux之我最常用的20条命令

    1.cd命令   这是一个非常基本,也是大家经常需要使用的命令,它用于切换当前目录,它的参数是要切换到的目录的路径,可以是绝对路径,也可以是相对路径.如: cd /root/Docements # 切 ...

  3. java单元测试的用法及原因

    1.ctrl+n  生成  Junit Test Case 2.选择文件夹 3.superClass  继承BaseUnitTest 4.next后 打勾选择需要单元测试的方法. 5.在生成的test ...

  4. python解释 yield 和 Generators(生成器)

    yield 和 Generators(生成器) 转自:http://www.oschina.net/translate/improve-your-python-yield-and-generators ...

  5. Python3列表中获取相同元素出现位置的下标

    前言 list: Python3的列表类型, 和其他语言中的数组类似 定义格式: l = ["a", "b", "c", "a&q ...

  6. .NetCore下使用EF DbFirst操作MySql

    新建.NetCore的控制台项目 使用Nuget安装Pomelo.entityframeworkcore.mysql 工程右键--->编辑.csproj文件,把以下内容写入到工程文件 <I ...

  7. c++ 整数和字符串的转化

    一.string转int的方式 采用最原始的string, 然后按照十进制的特点进行算术运算得到int,但是这种方式太麻烦,这里不介绍了. 采用标准库中atoi函数. "; int a = ...

  8. AJAX请求中出现OPTIONS请求

    背景 有一个前后端分离的VUE项目来发送ajax请求, 查看Nginx日志或使用Chrome Dev Tools查看请求发送情况时, 会看到每次调后台API的请求之前, 都会发送一个OPTIONS请求 ...

  9. bootstrape3 模态

    version 2 的模态 不能在 version 3 上面用. http://schy-hqh.iteye.com/blog/1995690

  10. RabbitMQ基础使用之集群构建

    简介 RabbitMQ是基于Erlang开发的一种消息队列服务,本篇文章主要部署三台机器用来实现集群的普通模式与镜像模式!欢迎大家吐槽交流学习! 特点 集群节点包括内存节点和磁盘节点,有了磁盘节点就支 ...