1

I found an example how to trigger an event if enter key pressed, but the problem is that this event triggered if any other button pressed at the same time. Especially, this problem is relevant for textarea, when Shift + Enter stands for expanding input vertically.

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

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

So the question is actually how to catch event, when ONLY Enter key pressed?

Majesty
  • 2,097
  • 5
  • 24
  • 55
  • `.bind()` is depricated, use [`.on()`](http://api.jquery.com/on/) instead. Secondly, in [`How to use a keypress event in angularjs`](https://stackoverflow.com/questions/17470790/how-to-use-a-keypress-event-in-angularjs), `EpokK` included HTML markup too, did you include that? – Alex Jun 21 '18 at 15:13

1 Answers1

1

You can simply add another check for that i-e whether shift is pressed using event.shiftKey and do accordingly. Something like

if(event.which === 13 && !event.shiftKey) {
scope.$apply(function (){
                    scope.$eval(attrs.ngEnter);
                });
...
}
Muhammad Usman
  • 10,039
  • 22
  • 39