Jump to content
COMFIED

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;

 

Share this post


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?

Share this post


Link to post
Share on other sites

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;


 

 

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×