html - html中的关键事件不会抛出函数
问题描述
我有一个简单的问题,但我找不到它的来源。
在 Angular 组件中,我想使用一些键盘事件。
这是我的代码:
keyDownUp(e){
//some code to test.
if (e.keyCode == 16){
if(this.shiftDown) {
this.shiftDown=false;
} else {
this.shiftDown=true;
}
}
}
<div (mouseup)="mouseup()" (mouseleave)="mouseleave()" (mousemove)="mousemove($event)" (keydown)="keyDownUp($event)" (keyup)="keyDownUp($event)" [ngClass]="{'disable-text': dragStatus ===true}">
这里鼠标事件起作用,但关键事件不起作用(该功能keyDownUp
未激活)。
我在语法中看不到任何错误。
解决方案
您需要专注于您的 div 才能发生关键事件。要使您的 div 具有焦点,您可以添加tabindex="0"
到您的 div 中。它会做的工作
推荐阅读
- c++ - 我的用于汇总数组元素的 C++ 代码无法正常工作
- python - 如何在 tf.keras 自定义损失函数中触发 python 函数?
- reactjs - 在安装包后导入包或在创建新的 React 应用程序时将其包含在 index.html 中有什么区别?
- javascript - 在 JavaScript 中打开和修改文件的函数
- xamarin - 如何在 xamarin 表单应用程序中正确实现 mapbox
- java - java.lang.UnsatisfiedLinkError 即使库和类有方法
- javascript - 返回第一个找到的值对象
- reactjs - 使用 history.push 进行呼叫转移
- python - tkinter 代码执行顺序问题
- c# - 调用泛型函数并分配泛型变量时编译错误