2

I use this code for copying paragraph when I clicked "1" on the keyboard:

$(document).keypress(function (e) {
if (e.which == 49) {
    var $temp = $("<input>");
    $("body").append($temp);
    $temp.val($('.one').children('p').text()).select();
    document.execCommand("copy");
    $temp.remove();
}
});

but what I would like to add real hotkeys like ctrl + alt + 1 for example

MoHamed HaSsn
  • 555
  • 1
  • 6
  • 20
  • 1
    You may want to check this out: [Keyboard shortcuts with jQuery](https://stackoverflow.com/questions/593602/keyboard-shortcuts-with-jquery) –  Mar 26 '19 at 01:34

3 Answers3

2

You can use keydown and keyup like below.

What this code below does is that on keydown, it sets the value of that key in the object keys to true. It also uses an if statement to check if those 3 keys are pressed. If so, it does something.

The 17, 18, and 49 are the keycodes for CtrlShift1

var keys = {};

$(document).keydown(function(e) {
    keys[e.which] = true;

    if (keys[17] && keys[18] && keys[49]) { // Ctrl + Alt + 1 in that order
      console.log("pressed");
    }
});

$(document).keyup(function(e) {
  delete keys[e.which];
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
Aniket G
  • 3,471
  • 1
  • 13
  • 39
2
$(document).keydown(function(e) {
    if (e.ctrlKey && e.altKey && e.which == 49) { // Ctrl + Alt + 1 in that order
      console.log("pressed");
    }
});

I like this one you can use shiftkey if you want as e.shiftKey

1

You can do this pretty easily with or without jQuery (I've included both versions). Just remember to prevent the default behavior with evt.preventDefault() as soon as you know you are interested in the keypress in case the user's browser has a hotkey that matches the one you want to use.

Here is the jQuery version

$(document).keydown(function(evt) {
    if (!evt.ctrlKey || !evt.altKey || evt.which < 48 || evt.which >57) {
      return;
    }

    evt.preventDefault();

  switch(evt.which) {
    case 49:
      console.log("hotkey 1");
      return;
    case 50:
      console.log("hotkey 2");
      return;
    case 51:
      console.log("hotkey 3");
      return;
    case 52:
      console.log("hotkey 4");
      return;
    case 53:
      console.log("hotkey 5");
      return;
    case 54:
      console.log("hotkey 6");
      return;
    case 55:
      console.log("hotkey 7");
      return;
    case 56:
      console.log("hotkey 8");
      return;
    case 57:
      console.log("hotkey 9");
      return;
    case 48:
      console.log("hotkey 0");
      return;
  }
});

Here is the native js version

window.addEventListener("keydown", function (evt) {
  if(!evt.altKey || !evt.ctrlKey || evt.which < 48 || evt.which > 57) {
    return;
  }
  evt.preventDefault();
  switch(evt.which) {
    case 49:
      console.log("hotkey 1");
      return;
    case 50:
      console.log("hotkey 2");
      return;
    case 51:
      console.log("hotkey 3");
      return;
    case 52:
      console.log("hotkey 4");
      return;
    case 53:
      console.log("hotkey 5");
      return;
    case 54:
      console.log("hotkey 6");
      return;
    case 55:
      console.log("hotkey 7");
      return;
    case 56:
      console.log("hotkey 8");
      return;
    case 57:
      console.log("hotkey 9");
      return;
    case 48:
      console.log("hotkey 0");
      return;
  }
}, false);
TxRegex
  • 2,347
  • 21
  • 20