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

1

3

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': ''
    };
});
Ashraf answered 13/5, 2015 at 15:5 Comment(6)
I don't know if i just don't understand what you want to do but your plunker works well for me.Nerveracking
Works well when you press only P + RIGHT ARROW. Appears message "P + RIGHT pressed!" But when you press "SHIFT + P + RIGHT ARROW should appear message "SHIFT + P + RIGHT pressed!" This not happens for me.Ashraf
Maybe a browser issue ? I'm using chrome on macOS and when i press "SHIFT + P + RIGHT ARROW" and release the keys i see both messages.Nerveracking
Thanks @Nerveracking .... very helpfull information. I am looking for a solution that will works on all major browsers. I have tried on Chrome, IE9 and Firefox on windows and didn't work for me.Ashraf
Since i actually can't reproduce, i'll try to test on my windows at home. If it still work, this will be hard to spot.Nerveracking
@Nerveracking I have found that it works with left shift button and does not work with right shift button. Can you post answer something like that the code is ok and that eventually is another problem with lef right shifts. I would like to accept your answer.Ashraf
N
2

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.

Nerveracking answered 20/5, 2015 at 0:22 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.