Search code examples
stringdelphitextclipboardtms-web-core

Copy Text to Clipboard in Delphi TMS Web Core Website


The VCL/FMX way of copying text to the clipboard doesn't work in TMS Web Core.

How can I copy text (Strings) to the clipboard in a Delphi TMS Web Core Website?


Solution

  • There are multiple ways to copy text to the clipboard in TMS Web Core:


    1. TWebClipboard (Non-Visual Component)

    Delphi TWebClipboard

    Add a TWebClipboard component to your form and then you can copy text to it by running:

    WebClipboard.CopyToClipboard('Your String to Copy to Clipboard');
    

    2. TWebClipboard Custom Method

    You can make your own procedure that creates and uses the TWebClipboard component from the WEBLib.Clipboard unit:

    procedure CopyTextToClipboard(aText: String);
    var
      WebClipboard: TWebClipboard;
    begin
      WebClipboard := TWebClipboard.Create(self);
      try
        WebClipboard.CopyToClipboard(aText);
      finally
        WebClipboard.Free;
      end;
    end;
    

    3. JavaScript function

    You can make a Delphi function that uses JavaScript to copy text to the clipboard:

    procedure CopyTextToClipboard(aText: String);
    begin
      asm
        window.navigator.clipboard.writeText(aText);
      end;
    end;