0

I have a method that gets called inside a div tag using ng-click="startChat()" - it works fine.

I would like the same function to be called there when pressing down a key, let's say 'p' in this case which has a keycode of 112.

I tried adding the following to the div:

ng-keydown="$event.keyCode == 112 ? startChat()"

I also tried the same with ng-keyupp but nothing happens. Does someone know why and how can I achieve the task?

1

3 Answers 3

3

You have to treat the action of the ng-keydown as a function call to either your $scope in the controller or a function in your link section of the directive. Code below works for me.

<input type="text" ng-keydown='doSomething($event)'>

$scope.doSomething= function($event){
    var key = $event.keyCode;
    console.log(key)
}
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for your example!
0

You can do this with ng-keypress. Call a function on this and check for the keyCode.

Comments

0

Try with directive as:

JS:

app.directive('myEnter', function () {
    return function (scope, element, attrs) {
        element.bind("keydown keypress", function (event) {
            if(event.which === 13) {
                scope.$apply(function (){
                    scope.$eval(attrs.myEnter);
                });

                event.preventDefault();
            }
        });
    };
});

HTML:

<div ng-app="" ng-controller="MainCtrl">
    <input type="text" my-enter="doSomething()">    
</div>

Font: How to use a keypress event in AngularJS?

Our simple keypress

Demo:

function myController($scope) {
  $scope.lastKey = "---"

  $scope.keypress = function($event) {
    $scope.lastKey = $event.keyCode
  };
    
}
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<div ng-app>
  <h2>Example</h2>
  <div ng-controller="myController">

      <p>The new ng-keydown directive works perfectly fine as long as it is bound to an input field. Type to see keyCode: </p>
      <input ng-keydown="keypress($event)">
          <p>last key: {{lastKey}}</p>
          <div ng-keydown="keypress($event)" style="border: 1px solid blue;">
<p>Focusing this div (blue border) and typing however does not work, in spite of the documentation saying that the context for ng-keydown can be anything: http://code.angularjs.org/1.1.3/docs/api/ng.directive:ngKeydown</p>
<p>I can't seem to get it to cooperate unless there is an input field:
<input>
    </p>
    <p>How can I use ng-keydown for the entire document or selected spans, etc.?
    </p>
          </div>
  </div>
</div>

7 Comments

What i meant was you have not considered to modify this answer as per the question is asked and this answer is really going to be downvoted quickly.
@Jai Why not answer the question ? Search the Internet , others will have the same problem with ng - keypress
check the question keydown is there.
Right and gave an alternative that will have the same effect ;)
Thanks for this but I would rather avoid creating a directive for this simple task. Is there an easier way to accomplish this? It's simple to call a method using ng-click, but is there really no easier way to call a existing method using a keypress instead?
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.