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

Question
-
I tried using document.body.onpaste but its never called. How would I register for the Paste event?
- Edited by Jeff SandersMicrosoft employee, Moderator Friday, June 22, 2012 7:07 PM wrong key combo in title
Thursday, June 21, 2012 4:29 PM
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"; } }
-JeffJeff Sanders (MSFT)
- Proposed as answer by Jeff SandersMicrosoft employee, Moderator Friday, June 22, 2012 7:22 PM
- Marked as answer by Dino He Friday, June 29, 2012 9:56 AM
Friday, June 22, 2012 7:22 PMModerator -
No it is not broken.
You cannot "Paste" into the body... only input elements.
-Jeff
Jeff Sanders (MSFT)
- Marked as answer by Jeff SandersMicrosoft employee, Moderator Tuesday, September 25, 2012 1:25 PM
Tuesday, September 25, 2012 1:25 PMModerator
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 FrameworkFriday, June 22, 2012 9:32 AMModerator -
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"; } }
-JeffJeff Sanders (MSFT)
- Proposed as answer by Jeff SandersMicrosoft employee, Moderator Friday, June 22, 2012 7:22 PM
- Marked as answer by Dino He Friday, June 29, 2012 9:56 AM
Friday, June 22, 2012 7:22 PMModerator -
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)
- Marked as answer by Jeff SandersMicrosoft employee, Moderator Tuesday, September 25, 2012 1:25 PM
Tuesday, September 25, 2012 1:25 PMModerator