M
M
Mikhail Voitenko2015-01-26 20:06:14
JavaScript
Mikhail Voitenko, 2015-01-26 20:06:14

How to paste text from clipboard in JavaScript?

You need to get to the contents of the buffer (text) and paste it into a variable in JS. If everything is clear with IE, then what about other browsers?
The bottom line is that the main application is written in GWT and the key combination is processed first. So despite the fact that the document already hangs eventListeneron 'paste' - it is the browser paste that does not work. From GWT comes only KeyboardEvent. I can create and call a paste event like

var pasteEvent = $doc.createEvent ("Event");
pasteEvent.initEvent ('paste', true, false);
$doc.dispatchEvent (pasteEvent);

, but it has an empty clipboardData value, which is used in the lisener I wrote about above to get the contents of the buffer (code):
$doc.addEventListener('paste', function(e) {
                 var clipboardData = e.clipboardData;
               console.log('Clipboard Plain Text: ' + 
clipboardData.getData('text/plain'));   });

Answer the question

In order to leave comments, you need to log in

3 answer(s)
Y
Yuri Puzynya, 2015-01-26
@3y3

You cannot do this using js - this is an unsafe action, you may have passwords in the buffer, no one should read them on the sly.
In general, everything has been written for a long time. Do you have problems with Google? stackoverflow.com/questions/400212/how-do-i-copy-t...

K
KOLANICH, 2015-01-26
@KOLANICH

You can listen to the paste from the buffer using the browser.

M
Mikhail Voitenko, 2015-01-28
@Firehanded

The question is closed. The reason turned out to be keyDownEvent.preventDefault();in the key combination handler from the GWT side.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question