J'ai passé beaucoup de temps à chercher une solution à ce problème aussi. Voici ce que j'ai trouvé jusqu'à présent:
Si vous voulez que vos utilisateurs d'être en mesure de cliquer sur un bouton pour copier du texte, vous pourriez avoir à utiliser le Flash.
Si vous voulez que vos utilisateurs appuyez sur Ctrl+C n'importe où sur la page, mais toujours copier xyz dans le presse-papiers, j'ai écrit un tout-JS solution dans YUI3 (bien qu'il pourrait facilement être porté avec d'autres cadres, ou raw JS si vous vous sentez particulièrement de dégoût de soi).
Il s'agit de créer une zone de texte de l'écran qui se mis en évidence dès que l'utilisateur appuie sur Ctrl/CMD. Quand ils ont frappé "C", peu de temps après, ils ont copier le texte caché. Si ils ont frappé un 'V', ils redirigé vers un récipient (de votre choix) avant de le coller événement se déclenche.
Cette méthode fonctionne bien, parce que pendant que vous écoutez de la touche Ctrl/CMD keydown n'importe où dans le corps, le 'A', 'C' ou 'V' keydown d'auditeurs seulement joindre la face cachée de la zone de texte (et non de l'ensemble du corps).
Il également n'a pas à casser les attentes des utilisateurs - vous seulement obtenir redirigé vers la case masqué si vous aviez rien n'est sélectionné pour la copie de toute façon!
Voici ce que j'ai eu à travailler sur mon site, mais il faut vérifier http://at.cg/js/clipboard.js pour les mises à jour si il y a de tout:
YUI.add('clipboard', function(Y) {
// Change this to the id of the text area you would like to always paste in to:
pasteBox = Y.one('#pasteDIV');
// Make a hidden textbox somewhere off the page.
Y.one('body').append('<input id="copyBox" type="text" name="result" style="position:fixed; top:-20%;" onkeyup="pasteBox.focus()">');
copyBox = Y.one('#copyBox');
// Key bindings for Ctrl+A, Ctrl+C, Ctrl+V, etc:
// Catch Ctrl/Window/Apple keydown anywhere on the page.
Y.on('key', function(e) {
copyData();
// Uncomment below alert and remove keyCodes after 'down:' to figure out keyCodes for other buttons.
// alert(e.keyCode);
// }, 'body', 'down:', Y);
}, 'body', 'down:91,224,17', Y);
// Catch V - BUT ONLY WHEN PRESSED IN THE copyBox!!!
Y.on('key', function(e) {
// Oh no! The user wants to paste, but their about to paste into the hidden #copyBox!!
// Luckily, pastes happen on keyPress (which is why if you hold down the V you get lots of pastes), and we caught the V on keyDown (before keyPress).
// Thus, if we're quick, we can redirect the user to the right box and they can unload their paste into the appropriate container. phew.
pasteBox.select();
}, '#copyBox', 'down:86', Y);
// Catch A - BUT ONLY WHEN PRESSED IN THE copyBox!!!
Y.on('key', function(e) {
// User wants to select all - but he/she is in the hidden #copyBox! That wont do.. select the pasteBox instead (which is probably where they wanted to be).
pasteBox.select();
}, '#copyBox', 'down:65', Y);
// What to do when keybindings are fired:
// User has pressed Ctrl/Meta, and is probably about to press A,C or V. If they've got nothing selected, or have selected what you want them to copy, redirect to the hidden copyBox!
function copyData() {
var txt = '';
// props to Sabarinathan Arthanari for sharing with the world how to get the selected text on a page, cheers mate!
if (window.getSelection) { txt = window.getSelection(); }
else if (document.getSelection) { txt = document.getSelection(); }
else if (document.selection) { txt = document.selection.createRange().text; }
else alert('Something went wrong and I have no idea why - please contact me with your browser type (Firefox, Safari, etc) and what you tried to copy and I will fix this immediately!');
// If the user has nothing selected after pressing Ctrl/Meta, they might want to copy what you want them to copy.
if(txt=='') {
copyBox.select();
}
// They also might have manually selected what you wanted them to copy! How unnecessary! Maybe now is the time to tell them how silly they are..?!
else if (txt == copyBox.get('value')) {
alert('This site uses advanced copy/paste technology, possibly from the future.\n \nYou do not need to select things manually - just press Ctrl+C! \n \n(Ctrl+V will always paste to the main box too.)');
copyBox.select();
} else {
// They also might have selected something completely different! If so, let them. It's only fair.
}
}
});
J'espère que quelqu'un d'autre trouve cela utile :]