4

I try to simulate Ctrl+V in javascript. I use firefox in Linux. What I do is:

var pressEvent = document.createEvent ("KeyboardEvent");    
pressEvent.initKeyEvent ("keypress", true, true, window, true, false, false, false, 86, 0);
var accepted=aTarget.dispatchEvent (pressEvent);

Somebody knows why it doesn't work??

Thanks

6
  • 4
    Security reasons? You can't just read the clipboard without the user letting you. Commented Feb 13, 2013 at 18:31
  • Did you find a solution for this one? Commented Jan 16, 2015 at 19:52
  • @JanDvorak how can the user let you do this? Commented Apr 26, 2017 at 14:44
  • @I'mBlueDaBaDee you could provide the user a textbox to paste into. Opacity:0 might work, but the CTRL+V does have to be there. Commented Apr 26, 2017 at 14:45
  • @JanDvorak Is there something like Notification.requestPermission() for that? Commented Apr 26, 2017 at 15:44

1 Answer 1

6

Copy and Paste and the clipoard itself are very protected. Unless there is explicit user action it won't work with simulations. If you make a script that simulates a paste inside a inputbox then submit that to your server, you might get some really private info from people clipboards. So...no, that won't work to my knowledge. I would love for you or someone else to prove me wrong and share the solution.

But do tell what you're planning to do and maybe there's a workaround it that doesn't involve this simulated action.

Sign up to request clarification or add additional context in comments.

2 Comments

I don't think it'll be a security problem, because it works in Windows.. What I want to do is cut some text and paste it in other place. Simulate ctrl+a and ctrl+x works fine but ctrl+v not.
@TheBrain I'm attempting to create a clipboard context menu option. When a user clicks one of the submenus, it pastes the associated text into whatever textbox/page is active.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.