-1

When I press ctrl + right arrow,

I want the following code to run:

alert ('ctr | + right');

I tried to implement it using js logic, but it was difficult.

Thanks for letting me know

my code

$(function() {
    $('body').keydown(function(e) {
       if(e.ctrlKey && e.which == 78 ){
            alert('ctrㅣ + n');
       }
    });
});

It was a conflict with the chrome shortcut. Thanks to those who answered I solved the problem Thanks for the hint.

this code is work 39 is right direction key

$(function() {
  $('body').keyup(function(e) {
    if (e.ctrlKey && e.which === 39) {
      alert('ctrㅣ + n');
    }
  });
});

Misha Akopov
  • 12,241
  • 27
  • 68
  • 82
hyun k
  • 191
  • 1
  • 10
  • 1
    If you show the javascript code you tried, it will be easier for people to show you where you went wrong. – ASGM Jan 05 '20 at 13:01
  • 1
    If you've tried to implement it already, could you post the code you've got? This will give people something to work on and help you. – Run_Script Jan 05 '20 at 13:02
  • 1
    Take a look at this link: https://stackoverflow.com/questions/2903991/how-to-detect-ctrlv-ctrlc-using-javascript – Pouria Moosavi Jan 05 '20 at 13:04
  • Thanks for the link, maybe I can do it with the link – hyun k Jan 05 '20 at 13:08
  • 1
    No way to override Ctrl + N in Chrome since it opens a new window and can no longer be intercepted by the client side JavaScript in the web page. – Ahed Kabalan Jan 05 '20 at 13:13

2 Answers2

1

try this:

$(function() {
  $('body').keydown(function(e) {
    if (e.ctrlKey && e.which === 69) {
      e.preventDefault();
      alert('ctrㅣ + n');
    }
  });
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
Carsten Løvbo Andersen
  • 26,637
  • 10
  • 47
  • 77
Nengak dakup
  • 88
  • 2
  • 11
0

Replace 78 with 69. Also in major browser Ctrl+N is shortcut for creating new document

$(function() {
  $('body').keydown(function(e) {
    if (e.ctrlKey && e.which === 69) {
      alert('ctrㅣ + n');
    }
  });
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
Misha Akopov
  • 12,241
  • 27
  • 68
  • 82
brk
  • 48,835
  • 10
  • 56
  • 78
  • It was a conflict with the chrome shortcut. I solved the problem with a hint on how you told me. – hyun k Jan 05 '20 at 13:28
  • 1
    Looks like you solved your problem - also want to mention if you are able to add an additional dependency, mousetrap js is really easy to use for this type of functionality. https://github.com/ccampbell/mousetrap – Jeff Tsui Jan 05 '20 at 13:35