Is it possible to listen for arrow keyspress using ng-keypress?

Matthew Harwood picture Matthew Harwood · Aug 23, 2014 · Viewed 24.9k times · Source

I'm trying to create an interaction similar to the konami code "up,up,down,down,a,b,a,b, enter" -> something happens.

Is it possible to listen for arrow keyspress using ng-keypress? it seems to not work?

html:

input( ng-keypress='changed($event)'  )

Js

$scope.changed = (evt) ->
    console.log(evt)

this will not log out arrow key events?

Do I have to roll out my own listeners on the window? if so how can I achieve this in angular?

Answer

Darshan P picture Darshan P · Aug 23, 2014

DEMO

$scope.key = function($event){
    console.log($event.keyCode);
    if ($event.keyCode == 38)
        alert("up arrow");
    else if ($event.keyCode == 39)
        alert("right arrow");
    else if ($event.keyCode == 40)
        alert("down arrow");
    else if ($event.keyCode == 37)
        alert("left arrow");
}

EDIT

Change it from ng-keypress to ng-keydown. DEMO

<input ng-keydown="key($event)" />