Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>This solution only works for keystrokes that would invoke the desired operation. It works by moving the user's cursor into a textarea element before the user finishes the relevant keystroke. It only works for text input. I've got this working in firefox and chrome. IE can use the clipboardData object (which is preferable to this hack).</p> <p>In your html somewhere you should create a textarea element with arbitrarily large rows and cols attributes. The '<strong>clipboard-textarea</strong>' element will be the holding area for the pasted and copied data. I hide the element using some style attributes.</p> <p>The script:</p> <pre><code>var desiredClipboardContents = 'It works'; function onCopyKeyPressed() { // The trick here is to populate the textarea with // the text you want copied before the user releases // the copy keystroke. var textarea = document.getElementById('clipboard-textarea'); textarea.value = desiredClipboardContents; textarea.focus(); textarea.select(); } function onPasteKeyPressed() { var textarea = document.getElementById('clipboard-textarea'); textarea.value = ''; textarea.focus(); // The trick here is to delay slurping the content // that arrives in the textarea element until after // the paste keystroke is completed. The 750 ms timeout // provides the necessary delay. setTimeout("finishedPasting", 750); } function finishedPasting() { var textarea = document.getElementById('clipboard-textarea'); alert("Received from clipboard-paste: " + textarea.value); } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload