Jump to content

Copying Text to Clipboard


Recommended Posts

Any help on how I can click a label/ text to copy to clipboard?

Here is my code, but its not working.

procedure TForm1.W3Label1Click(Sender: TObject);
begin

     W3Label1.Caption:= '<id="ACC">1234';

   var document.getElementById('ACC');
    copyText.select();
    document.execCommand('copy');
    ShowMessage("Copied the text: " + copyText.value);
end;

 

Link to post
Share on other sites

This works and copies text to clipboard

     browserapi.document.designMode := 'on';
     asm
         navigator.clipboard.writeText("copy this text");
     end;
 

I'm still unable to introduce a string variable from label or input box to replace "copy this text" - Any idea how I can do that?

Link to post
Share on other sites
  • Moderators

Browsers pick up selected text in <textarea> and <p> type of html elements, not necessarily in each and every other type. Also they will not mess with existing clipboard data unless initiated by a user action (like clicking a button).

Bit of a roundabout way of doing this, but this works

(form with W3Label1 and W3Button1)

procedure TForm1.W3Button1Click(Sender: TObject);
begin
var xCopy : Procedure(S:String);
asm
@xCopy = function copyToClipboard(text) {
    if (window.clipboardData && window.clipboardData.setData) {
        // IE specific code path to prevent textarea being shown while dialog is visible.
        return clipboardData.setData("Text", text);

    } else if (document.queryCommandSupported && document.queryCommandSupported("copy")) {
        var textarea = document.createElement("textarea");
        textarea.textContent = text;
        textarea.style.position = "fixed";  // Prevent scrolling to bottom of page in MS Edge.
        document.body.appendChild(textarea);
        textarea.select();
        try {
            return document.execCommand("copy");  // Security exception may be thrown by some browsers.
        } catch (ex) {
            console.warn("Copy to clipboard failed.", ex);
            return false;
        } finally {
            document.body.removeChild(textarea);
        }
    }
};
end;
xCopy(W3Label1.Caption);
end;


 

 

Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...