1

Hi I've got follow code:

angular.module("myApp", []).controller("myController", function($scope) {
  $scope.pressedKey = function(keyObj) {
    $scope.myKey = keyObj.key;
  }
});
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>

<div ng-app="myApp" ng-controller="myController">
  <input ng-keypress="pressedKey($event)"><br>{{myKey}}
</div>

I use the ng-keypress on an input for detecting if there was a key-event and which key was clicked. I need all numbers and letters and also the enter and delete key. Now, the numbers, letters and the enter works fine, but when I click the delete key, nothing happens. How can I detect it also with angular?

Thanks & cheers.

webta.st.ic
  • 4,781
  • 6
  • 48
  • 98

2 Answers2

7

Use ng-keydown instead of ng-keypress:

angular.module("myApp", []).controller("myController", function($scope) {
  $scope.pressedKey = function(keyObj) {
    $scope.myKey = keyObj.key;
  }
});
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>

<div ng-app="myApp" ng-controller="myController">
  <input ng-keydown="pressedKey($event)"><br>{{myKey}}
</div>

You can read more about the difference between keydown and keypress here.

Community
  • 1
  • 1
Martijn Welker
  • 5,575
  • 1
  • 16
  • 26
0

it's simpler than that.

keypress doesn't detect the delete key. Use keydown instead and it will.

Noé Rubinstein
  • 786
  • 6
  • 17