是否可以使用 ng-keypress 监听箭头键? [英] Is it possible to listen for arrow keyspress using ng-keypress?

查看:25
本文介绍了是否可以使用 ng-keypress 监听箭头键?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试创建类似于 konami 代码向上、向上、向下、向下、a、b、a、b,输入"的交互 -> 发生了一些事情.

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

是否可以使用 ng-keypress 监听箭头键?它似乎不起作用?

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?

推荐答案

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

编辑

将其从 ng-keypress 更改为 ng-keydown. DEMO

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

这篇关于是否可以使用 ng-keypress 监听箭头键?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆