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

Vincent Tallier picture Vincent Tallier · Sep 16, 2014 · Viewed 9.7k times · Source

I'm trying to detect when the user presses Ctrl + V in JavaScript.

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

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

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

Any idea what I'm doing wrong?

EDIT - 2014-09-17 - need the clipboard content.

I cannot just rely on key detection, because I need the clipboard content which is only available through a paste event (there is no clean other way to access it). In this JSFiddle, I get the event and display the text (works on Chrome only)

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

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

Answer

Todd Mark picture Todd Mark · Sep 16, 2014

This is my JSFIDDLE. It worked well on Chrom & Firefox &IE10 Here is code:

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