-1

I've looked on the internet for this and all I can find are depreciated functions so before posting please check to make sure that the code you suggest isn't depreciated.

I've found this and tried it: https://developer.mozilla.org/en-US/docs/Web/API/KeyboardEvent/KeyboardEvent

$(document).ready(function () {
    var x = new KeyboardEvent("FormatCode", deprectiatedArgument);
});

But after further inspection the KeyboardEventInit is depreciated.

I would like to create an event on pres of the CTRL + K keys.

Andrew Kilburn
  • 2,131
  • 5
  • 29
  • 63
  • 1
    Possible duplicate of [Trigger a keypress/keydown/keyup event in JS/jQuery?](http://stackoverflow.com/questions/3368578/trigger-a-keypress-keydown-keyup-event-in-js-jquery) – blgt Jul 20 '16 at 14:23
  • 1
    @blgt It's not a possible duplicate at all. The link which you provided included the person askingt o detect for a key press of one key. I am asking to detect for the key press of CTRL + K. CTRL is not detected by keypress(). Perhaps read the question first instead of just the title – Andrew Kilburn Jul 20 '16 at 14:25
  • The question is asking about programatically triggering (note: not detecting) a keyboard event, which the linked question *does* answer. Adding a modifier to a thus created event is trivial. As currently worded, it's exactly duplicate. You should edit in an explanation as to what the difference is – blgt Jul 20 '16 at 14:35

4 Answers4

2

You have a specific key code for every button on the keyboard. All of them are here http://keycode.info/.

$(document).keyup(function(e) {
    if (e.keyCode === 13) function();   // enter
    if (e.keyCode === 27) function();   // esc
});
Zakaria Acharki
  • 65,304
  • 15
  • 70
  • 95
aleksa95
  • 69
  • 3
1

Here's a vanilla JS solution to detect a CTRL + k keypress event:

UPDATED to also trigger the event.

document.addEventListener("keypress", function(e) {
  if ((e.ctrlKey || e.metaKey) && (e.keyCode == 11 || e.keyCode == 75)) {
    alert("ctrl+k!");
  }
});


document.getElementById("trigger").addEventListener("click", function(){
  //trigger a keypress event...
  var e = document.createEvent('HTMLEvents');
    e.initEvent("keypress", false, true);
    e.ctrlKey = true;
    e.keyCode = 75;
  document.dispatchEvent(e);
});
Press <kbd>ctrl+k</kbd> or
<a href="#" id="trigger">trigger the event</a>
Moob
  • 13,971
  • 1
  • 32
  • 45
  • Answer explains how to detect, not how to trigger, the event – blgt Jul 20 '16 at 14:42
  • @blgt That was not clear in your question. I've updated my answer to include an example of _triggering_ the event. – Moob Jul 20 '16 at 15:28
0

you can use a library called shortcut.js .. here is a link to their source code for downloading: http://www.openjs.com/scripts/events/keyboard_shortcuts/shortcut.js

then run ur code by making this function:

shortcut.add("Ctrl+K",function() {
    alert("Hi there!");
});

and here is the documentation : http://www.openjs.com/scripts/events/keyboard_shortcuts/

hope that can help.

shireef khatab
  • 947
  • 2
  • 13
  • 31
-1
$(document).ready(function () {
    var bool = false;
    $(document).keydown(function (e) {
        if (e.keyCode === 17) {
            bool = true;
        }
        if (bool == true && e.keyCode == 75) {
            alert("");
        }
    });
    $(document).keyup(function (e) {
        if (e.keyCode === 17) {
            bool = false;
        }
    });
});

This is how me and a friend got it working

Andrew Kilburn
  • 2,131
  • 5
  • 29
  • 63