JSFiddle - React, Tailwind, and code Playground

by Gabriel Le Breton

HTML

<textarea id="textArea1"></textarea>
<br />
<button type="button" id="magic-button">Do magic</button>

JavaScript

// catch the "ctrl" combination keydown
$.ctrl = function(key, callback, args) {
    $(document).keydown(function(e) {
        if(!args) args=[]; // IE barks when args is null
        if(e.keyCode == key && (e.ctrlKey || e.metaKey)) {
            callback.apply(this, args);
            return false;
        }
    });
};

// put your data on the textarea and select all
var performCopy = function() {
    $("#textArea1").text('PUT THE TEXT TO COPY HERE. CAN BE A FUNCTION.');
    $("#textArea1")[0].focus();
    $("#textArea1")[0].select();
};

// bind CTRL || CMD + C
$.ctrl('C'.charCodeAt(0), performCopy);

$("#magic-button").click(performCopy);