首页 > 解决方案 > 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未激活)。

我在语法中看不到任何错误。

标签: htmlangulartypescript

解决方案


您需要专注于您的 div 才能发生关键事件。要使您的 div 具有焦点,您可以添加tabindex="0"到您的 div 中。它会做的工作


推荐阅读