Skip to main content
Line
Inspiring
October 30, 2020
Question

Anyone know the code to copy text?

  • October 30, 2020
  • 1 reply
  • 220 views

I want to put a special code in my game and if you click a button, it will copy the text to your memory.

 

Do you know what the script is to do that?

 

Thanks ahead,

 

Line

    This topic has been closed for replies.

    1 reply

    JoãoCésar17023019
    Community Expert
    Community Expert
    October 30, 2020

    Hi.

     

    Supposing you have a button in the main timeline (called yourButton) created with Animate's drawing and design tools and a TextInput component instance called yourInput, the code could be something like this:

    this.yourButton.on("click", function(e)
    {
    	var input = document.getElementById("yourInput");
    	input.select();
    	document.execCommand("copy");
    });


    I hope it helps.

    Regards,
    JC

    Legend
    October 30, 2020

    Per the MDN page on document.execCommand(), "This feature is obsolete. Although it may still work in some browsers, its use is discouraged since it could be removed at any time. Try to avoid using it."

     

    The modern way to copy to the clipboard is using the clipboard API. However, IE11 doesn't support this. A properly robust solution would be to check for the clipboard API and use that if available. If it's not available, check for and use execCommand. If that's not available, do nothing.

     

    https://flaviocopes.com/clipboard-api/

     

     

     

    Line
    LineAuthor
    Inspiring
    November 2, 2020

    Thanks ClayUUID,

     

    I appreciate your constant attention!

     

    In regards to what JoãoCésar wrote, is that included? I'm surprised to read that the feature is obsolete.. i feel like being able to copy some text. like a coupon code or a password.. or something you just wouldn't want to have to type out, would be a capability people would want to work. Click here to copy.. I've seen it so many times on sites.. Is everyone just using it until it's removed everywhere?

     

    With me thinking that.. I wonder if my initial inquiry was written incorrectly and I am misunderstood..

     

    My game is a canvas game for phone/desktop play.. I just want some text to be saved to memory as if it were something you'd manually highlight and copy to paste somewhere else .

     

    Thanks again,

     

    Line

     

    Line

    //Signature// I'm a creator. I love illustration, designing, animating , writing, voice overs, acting, making games .. Woo woo!