How to detect shift + right arrow + some other key in angular

不问归期 提交于 2019-12-10 17:53:17

问题


I have a problem to detect shift + right arrow + p in angular.
I am using angular 1.2.3 I have no problem to detect only right arrow + p but when the shift comes into game something brakes

The question is hot to detect situation when 3 keys are pressed: SHIFT + RIGHT ARROW + P

Here is a working example on plunker

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

app.controller('MainCtrl', function($scope, $timeout) {
    /**
     * 39 (right arrow)
     * 80 (p)
     */
    var map = {39: false, 80: false};

    $scope.onKeyUp = function(event){
        if (map[39] && map[80]) {
            $scope.data.message1 = "P + RIGHT pressed!";
            $timeout(function(){
               $scope.data.message1 = '';
            }, 1000);
        }

        if (event.shiftKey && map[39] && map[80]) {
            $scope.data.message2 = "SHIFT + P + RIGHT pressed!";
            $timeout(function(){
               $scope.data.message2 = '';
            }, 1000);
        }

        var keyCode = getKeyboardEventCode(event);
        if (keyCode in map) {
            clearKeyCode(keyCode);
        }
    };


    $scope.onKeyDown = function(event){
        var keyCode = getKeyboardEventCode(event);
        if (keyCode in map) {
            map[keyCode] = true;
        }
    }


    var getKeyboardEventCode = function (event) {
        return parseInt((window.event ? event.keyCode : event.which));
    };

    function clearKeyCode(code){
        map[code] = false;
    }

    $scope.data = {
      'message1': '',
      'message2': ''
    };
});

回答1:


As said in the comment. Your code works actually well . Using chrome on macOS, when i press "SHIFT + P + RIGHT ARROW" and release the keys i see both messages.



来源:https://stackoverflow.com/questions/30218752/how-to-detect-shift-right-arrow-some-other-key-in-angular

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!