2

I am building a webapp where a user can click on any cell in a table. On click, the text of that cell is copied and will pop up inside a disabled textarea in a fancybox, and the textarea is selected in js. The user is then invited to hit CTRL-C to copy the text that is now inside the selected textarea.

I would like to combine 2 things together when CTRL-C happens: (1) Copy the selected text (2) Close the fancybox

I can trap the CTRL-C using jquery hotkeys, but once I use that to call the fancybox close method, the event is no longer propagating. is there anyway to use both. My code is:

$(document).bind('keydown', 'ctrl+c', function(event){   
    $.fancybox.close(); // if this is commented out, CTRL-C works just fine
    return true;
});

I have explained numerous times how this question is different from the others. I indicated in my original question that I can detect the keystrokes. The problem was how to detect that, let the action continue and also to take another action. I provided an answer below, which I figured out on my own.

Mark Salamon
  • 569
  • 1
  • 8
  • 21
  • Where do you expect the event to propagate to? In your code, it's already reaching the document object. Also, why include `return true;`? – Brad M Jun 26 '13 at 18:27
  • I was hoping the "return true" would propagate the CTRL-C event after the fancybox was closed. :) Unfortunately, my knowledge of event handling in js is rather limited. If there is another way to code this to achieve my goal, that would be greatly appreciated. – Mark Salamon Jun 26 '13 at 18:35

1 Answers1

4

look at this solution from How to detect that Ctrl+R was pressed?

$(document).keydown(function(e) {
    if (e.keyCode == 65 && e.ctrlKey) {
        alert('ctrl A');
    }
});

Edit:

<textarea>test</textarea>
<p id="test"></p>

$(document).keydown(function(e) {
    if (e.keyCode == 67 && e.ctrlKey) {
        /* close fancybox here */

        $("#test").text("copied!"); /* optional */
    }
});
Community
  • 1
  • 1
raptor
  • 760
  • 7
  • 18
  • I am successfully detecting that CTRL-C has been pressed. The issue is that I want the CTRL-C to continue, and copy the selected text, AND also to close the fancybox. I am not sure how the CTRL+R thread helps with that. – Mark Salamon Jun 26 '13 at 18:37
  • I'm still not sure that I understand. Your edited code appears to intercept the CTRL+C, then take the #test element and add the text "copied!" to it. I am trying to take the pre-selected text from the #test element, copy it to the clipboard AND close the fancybox where the #test element is located. – Mark Salamon Jun 26 '13 at 20:23
  • 1
    I figured it out. Instead of capturing the keydown, I capture the keyup. The keydown copies the text to the clipboard. The keyup closes the fancy box and unbinds the CTRL+C capture. – Mark Salamon Jun 27 '13 at 02:53
  • Your comment helped me better than the answer @MarkSalamon thanks! +1 to your question – Jorge Campos Aug 25 '15 at 14:18