如何观看在Angularjs的关键preSS组合? [英] How to watch for a keypress combination in Angularjs?

查看:110
本文介绍了如何观看在Angularjs的关键preSS组合?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图让我的控制器来监视键的组合。为了便于讨论,让我们说:上限上下上下左右左右B A。我怎样才能得到角度看出来的,这些不分场合在页面的用户目前是什么?

I'm trying to get my controller to watch for a combination of keys. For argument's sake, let's say: up up down down left right left right b a. How can I get angular to look out for these regardless of where in the page the user currently is?

推荐答案

看起来你可以使用的 NG-的keydown 来做到这一点。

Looks like you can use the ng-keydown to do this.

下面是工作plunker

有关此示例中,我只是约束 NG-的keydown <身体GT; 。作品pretty好赶上全球所有的键盘事件。

For this sample, I just bound ng-keydown to <body>. Works pretty well to catch all the keyboard events globally.

由于@charlietfl指出, NG-的keydown 注册了大量的键盘事件等等,使这个有用的将是大量的工作。例如,如果你想监听的组合(如 CTRL + 研究),那么 CTRL 键会注册很多次了。

As @charlietfl points out, ng-keydown registers a lot of keyboard events so to make this usable would be a lot of work. For example, if you were trying to listen for a combination (like ctrl + r), then the ctrl key will register many times.

JS:

var myApp = angular.module('myApp', []);

myApp.controller('Ctrl', function($scope) {


    $scope.keyBuffer = [];

    function arrays_equal(a,b) { return !(a<b || b<a); }

    $scope.down = function(e) {

      $scope.keyBuffer.push(e.keyCode);

      var upUp = [38, 38, 40, 40, 37, 39, 37, 39, 66, 65];
      if (arrays_equal(upUp, $scope.keyBuffer)) {

        alert('thats it!');
      }
    };

  });

HTML:

<body ng-controller="Ctrl" ng-keydown="down($event)">

这篇关于如何观看在Angularjs的关键preSS组合?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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