5.20231.904
5.20231.904

Clipboard

The Clipboard class allows you to customize the behavior of the clipboard by modifying its contents using the static methods copy and paste.

Note: The Clipboard class cannot initiate clipboard operations; it can only modify the contents of the clipboard after an operation has been initiated by a user.

The code below listens for the keydown event and checks the keystrokes for copy and paste operations. The copy method takes a string as an argument, while the paste method takes a callback function, and copies and pastes text.

The paste method only works if invoked immediately after the user pressed a clipboard paste command (like 'ctrl+v')

Example
rootElement.addEventListener('keydown', function(e) {

  // copy: ctrl+c or ctrl+Insert
  if (e.ctrlKey && (e.keyCode == 67 || e.keyCode == 45)) {
    var text = this.getClipString();
    Clipboard.copy(text);
    return;
  }

  // paste: ctrl+v or shift+Insert
  if ((e.ctrlKey && e.keyCode == 86) || (e.shiftKey && e.keyCode == 45)) {
    Clipboard.paste(function (text) {
      this.setClipString(text);
    });
    return;
  }
});