TheOpti
TheOpti

Reputation: 1661

Catch combination ctrl + n before internet browser using angular

I am using AngularJS in my application and I want to use combination Ctrl+N to go to state and view responsible for creating new objects in my application. The problem is that this combination opens a new windows in my internet browser.

The question is: is it possible to prevent this combination and use it to go to a new view in my app?

I know about ng-keyup directive, what's more I can catch single keys like Shift or Ctrl, but I would like to catch combination Ctrl+N and go to a new view.

Right now I have following code:

HTML:

ng-keyup="keyPress($event)

Angular:

$scope.keyPress = function(e) {
    e.preventDefault();
    if (e.ctrlKey) {
        var i = 2;
    }   
};

Unfortunately method preventDefault() doesn't work, and pressing Ctrl+N still opens a new window.

Upvotes: 0

Views: 1349

Answers (1)

Pascal
Pascal

Reputation: 122

I think you can use preventDefault() at this point. The character n should be the keycode 78

 $(document).keydown(function (objEvent) {
  if (objEvent.ctrlKey && objEvent.keyCode== 78) {
   objEvent.preventDefault();

   ...
 });

edit

Upvotes: 2

Related Questions