0

I have the following code:

index.html:

<a href="" class=”buttonone" ng-click="tool.disc()">
    <i class=”buttonone"></i>
</a>

controller.js:

angular.module(’app')

How can I make so that when someone presses a keystroke example 'enter key' the tool.disc() method gets triggered? I suppose I can use angularjs ngKeydown directive but I dont know how to implement it in the code example above. Could someone try to help? Thanks!

9
  • ngKeypres ? docs.angularjs.org/api/ng/directive/ngKeypress I think it only works on input element. Commented Jul 3, 2015 at 14:26
  • 1
    It work on any element @enguerranws Commented Jul 3, 2015 at 14:27
  • Feel free to try it : plnkr.co/edit/FsN7Pxy4OEm7ng3jmADG?p=preview Commented Jul 3, 2015 at 14:32
  • It's just logic : you can listen to events on any elements, but how can you trigger keypress on an <a> or an <img> element ? Commented Jul 3, 2015 at 14:34
  • To trigger a keypress on an element it must be in focus. You would need to add tabindex="1" or the following css to the element element:focus{ outline: 0; } Commented Jul 3, 2015 at 14:38

3 Answers 3

1

You cannot directly call the function specifically on Enter key press, use a function to check if the Enter key is pressed and then call your function.

You cannot listen Keypress event on anchor.

HTML:

<input type="text" ng-keypress="myFunc(e)" />
<!--               ^^^^^^^^^^^^^^^^^^^^^^^ -->
<i class=”buttonone"></i>

Controller:

$scope.myFunc = function (event) {
    if (event.keyCode === 13) {
        console.log('Enter key pressed');

        $scope.tool.disc(); // Call your function here
    }
};
Sign up to request clarification or add additional context in comments.

3 Comments

You can't listen keypress / keydown on <a> tag.
Why would you listen to keypress on an anchor ? You just can't "type in" an anchor.
Thanks alot for your kind help! I have several links, is it possible to add it to a div around them (or some other solution) and when pressing left / right arrow keys, navigate which one is in focus? Then when I press enter on one of them, it should trigger its function? Sorry if its advanced and to much to ask but you're a lifesaver if you make it happen!
0

The element needs to be in focus to work. To make this example work you will need to click the link and then press a key.

<a href="" ng-keydown="keydown($event)">Keydown</a> 

$scope.keydown = function (event) {
    if (event.keyCode === 13) {
        console.log('Enter key pressed');
    }
};

To auto focus on the element once loaded you could use

element[0].focus()

Otherwise, consider the follow:

$document.addEventListener('keyup', function (e) { console.log(e) })

3 Comments

Any live example of the first part working ? Can't make it work, even after focusing it or clicking it.
Thanks ! I don't why, for any reason, the same code doesn't work on Plunkr / JSFiddle.
Thanks alot for your kind help! I have several links, is it possible to add it to a div around them (or some other solution) and when pressing left / right arrow keys, navigate which one is in focus? Then when I press enter on one of them, it should trigger its function? Sorry if its advanced and to much to ask but you're a lifesaver if you make it happen!
0

ng-keypress or ng-keydown can do the trick :

 <div ng-keypress="yourFunction($event)> </div>

And then in your function, you can test the code of the key with

$event.keyCode;

For example, if you want to chech on enter :

$scope.yourFunction = function(event){
  if(event.keyCode == 13){
     console.log("Enter press");
  }
}

Edit :

Because sometime it doesn't work as much as angular wanted it, there is a directive :

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();
            }
        });
    };
});

Here this one is for enter, you can change the keycode if you need. To use it you can simply do this in every element :

<a href="" class="buttonone" ng-enter="function()" ng-click="tool.disc()">
    <i class=”buttonone"></i>
</a>

8 Comments

I think it would be interesting to attach it to the body, to listen every keypress event ?
It's depends on the use. If you need to the all page, you can do like this yeah. But sometime you juste have to listen an input or something and it's not necessary to call this function all the time. It can even be a problem if you press the key when you don't need to do the statement in the function.
That's for sure. But if the user focuses another part of the website (another div, i.e.), I'm not sure the keypress would work ? By the way, I can't make it work on <div> element. Any live example ?
Well, the example doesn't work on Chrome / Mac (even when I add tabindex="1").
|

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.