Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

like image 805
Vincent Tallier Avatar asked Sep 16 '14 15:09

Vincent Tallier


People also ask

How to detect Ctrl v in JavaScript?

To detect Ctrl+V, Ctrl+C using JavaScript, we can listen to the keyup and keydown events. let ctrlActive = false; let cActive = false; let vActive = false; document. body. addEventListener("keyup", (event) => { if (event.

How do you check if Ctrl C is pressed?

It indicates if Ctrl was pressed at the time of the event, like this: $(document). keypress("c",function(e) { if(e. ctrlKey) alert("Ctrl+C was pressed!!"); });

How do I disable Ctrl C in HTML?

To disable "Ctrl + C" put this script. function keypressed() {;return false;}document.


1 Answers

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("!");
    }
});
like image 165
Todd Mark Avatar answered Sep 18 '22 13:09

Todd Mark