2

I need to detect keys pressed on a div. In this plunk I have a div, but if I first click on it and then press a key (for example Del) then no event happens. How to make this work?

HTML

Click on the div and press a key:
<div ng-keypress="pressedKey($event)">

</div>
Key pressed: {{myKey}}

Javascript

angular.module("app", [])

.controller('ctl', function($scope) {

    $scope.pressedKey = function(event) {
        $scope.myKey = event.key;
      }

});
ps0604
  • 1,227
  • 23
  • 133
  • 330

1 Answers1

3

The div needs to be focusable. Just add

tabindex="0"

as an attibute on the div.

Demo

JB Nizet
  • 678,734
  • 91
  • 1,224
  • 1,255