locked
How to detect when the user presses the CTRL+V (Paste) keyboard shortcut?

Answers

  • Phil,

    You can trap the key down and look for the ctrl+C or V combination:

             args.setPromise(WinJS.UI.processAll());

             document.body.addEventListener("keydown", onkeydown);

     function onkeydown(event) {
            if (event.ctrlKey === true && event.key==="c") {
                var str = "ho";
            }
        }

    -Jeff

    Jeff Sanders (MSFT)

    Friday, June 22, 2012 7:22 PM
    Moderator
  • No it is not broken.

    You cannot "Paste" into the body... only input elements.

    -Jeff


    Jeff Sanders (MSFT)

    Tuesday, September 25, 2012 1:25 PM
    Moderator

All replies

  • Hi,

    I would like to point out that Ctrl+C is used to copy instead of to paste. So please handle the oncopy event.

    In addition, to add an event handler in JavaScript, you can invoke addEventListener like this:

                document.body.addEventListener("copy", oncopy);
                document.body.addEventListener("paste", onpaste);

    Best Regards,

    Ming Xu.


    Please mark the replies as answers if they help or unmark if not.
    If you have any feedback about my replies, please contact msdnmg@microsoft.com.
    Microsoft One Code Framework

    Friday, June 22, 2012 9:32 AM
    Moderator
  • I meant the Paste CRTL+V shortcut :) Have you tried this yourself? It did not work with document.body.onpaste... which should be the same as the addEventListener approach?
    Friday, June 22, 2012 9:40 AM
  • Phil,

    You can trap the key down and look for the ctrl+C or V combination:

             args.setPromise(WinJS.UI.processAll());

             document.body.addEventListener("keydown", onkeydown);

     function onkeydown(event) {
            if (event.ctrlKey === true && event.key==="c") {
                var str = "ho";
            }
        }

    -Jeff

    Jeff Sanders (MSFT)

    Friday, June 22, 2012 7:22 PM
    Moderator
  • so the onpaste / addEventHandler("paste") for the document.body is broken at the moment?
    Tuesday, September 25, 2012 9:59 AM
  • No it is not broken.

    You cannot "Paste" into the body... only input elements.

    -Jeff


    Jeff Sanders (MSFT)

    Tuesday, September 25, 2012 1:25 PM
    Moderator