0

I need to update $scope.CtrlKeypressed value if user pressed alt key it is set to try if he release alt key then ift will be set false but I am stuck here on release. It is not working i am using following code. I unable to do this with angular then I code jQuery code for it but this is also not working

 document.body.addEventListener('keydown', function (e) {
        if (e.keyCode==18)
        {
            alert("Keydown")
            $scope.ISCtrlPressed = true;
            $scope.$apply();

        }
    });


    document.body.addEventListener('keyup', function (e) {


            $scope.ISCtrlPressed = false;
            $scope.$apply();

    });

Can someone guide me how I need to work for this?

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Coder
  • 240
  • 2
  • 4
  • 20

2 Answers2

3

Look at this codepen with your code modified. You will get into console the I was pressed message as long as you keep the key pressed and receive an alert when release the key. It also depends on your operating system and how you have configured the keyboard(number of repetitions if kept pressed).

http://codepen.io/TunderScripts/pen/YpxWLR?editors=0011#0

document.body.addEventListener('keydown', function(e) {
  if (e.keyCode == 18) {
    console.log('I was pressed');

  }
});

document.body.addEventListener('keyup', function(e) {

  alert('aaaa come back')

});
Dinca Adrian
  • 1,190
  • 11
  • 21
  • Hi Dinca Adrian.Thanks . but can you please tell me but you modified in my code I am unable to find it – Coder Nov 26 '16 at 11:10
  • If you want to use it with angular recommend to do a directive and bind the keydown event there. You can find here some ideas http://stackoverflow.com/questions/15704771/angularjs-ng-keydown-directive-only-working-for-input-context – Dinca Adrian Nov 26 '16 at 11:25
0

<script>
    document.body.addEventListener('keydown', function (evt) {
        if (evt.keyCode == 18)
        {
            alert("Keydown");
            alert("Keyup");
        }
    });
</script>
Sher Singh
  • 279
  • 3
  • 13