10

I've set up a Plunker with some example code: http://plnkr.co/edit/upExTHzpkwtZ45mdikFr?p=preview

The very succinct question is: I'm trying to use AngularJS' ng-keyUp directive. In the docs I've only seen is used on an input, although I'm trying to capture keypresses anywhere on the page rather than just inside an input box. Like so:

// view

<div ng-keyup="keyPress($event)">
     // The bulk of my controller's view goes in here
</div>

// inside controller

$scope.keyPress = function(e){
     console.log(e);
}

Again, see the Plunker above for an example. Currently, it doesn't work at all and nothing is logged. How can I get it to work properly?

1
  • I think that you should rather use ng-click: <div ng-click="keyPress($event)"> because use on your page just click on your div. But maybe I don't understand your case. Commented Nov 11, 2013 at 8:31

2 Answers 2

24

Try to give the div element focus, or use tabindex="1"

Sign up to request clarification or add additional context in comments.

2 Comments

Perfect, tabindex="1" got it working. It had a weird :focus outline around it when I used this though, which I fixed by changing the css of .container:focus {outline: 0;}
this method worked for me first I have to click on div then only keys are detected. I am working on Django and using templates.
0

There is always good old vanilla coffee-script '.'

document.onkeypress = (e) ->
  console.log String.fromCharCode(charCode)

Comments

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.