How can I detect CTRL-V in JavaScript for IE and firefox

I am trying to detect when a user presses Ctrl + V on JavaScript.

jQuery(document).on('paste', function() {alert('text pasted!')}) 

This works well with Chrome (v37). But it does not work with Firefox (v32) and IE (v11), as you can try this jsfiddle:

http://jsfiddle.net/7N6Xq/410/

Any idea what I'm doing wrong?

EDIT - 2014-09-17 - clipboard content required.

I can't just rely on key detection because I need clipboard content that is accessible only through the paste event (there is no other way to access it). In this JSFiddle, I get the event and display the text (only works on Chrome)

http://jsfiddle.net/7N6Xq/412/

My ultimate goal is to get the image from the clipboard and send it directly to the server.

+7
javascript jquery paste
source share
3 answers

This is my JSFIDDLE . It worked great on Chrom and Firefox & IE10 Here is the code:

 $(document).keydown(function (event) { if (event.ctrlKey && event.keyCode == 86) { alert("!"); } }); 
+6
source share

You cannot just insert displayed text. You must create an input area where they can embed, <input> or <textarea> or similar. It inherits the onpaste handler, so you do not need to install it directly on the input element, but it does not work outside of one in most browsers.

See the fiddle here: http://jsfiddle.net/Lznvm8x9/

I am on a Mac, so I can not test IE, but it works in Firefox, as well as in Safari and Opera.

If you want to detect control-V as a key chord independent of the actual paste function, you can use keydown / keyup , but then you get into cross-platform problems - do you still want to be control-V on a Mac, where paste usually is the -V command, etc.

+2
source share

Use keydown and keyup + key codes instead of your actual approach.

+2
source share

All Articles