I'm struggling to enable the copy & paste feature in my terminal using xterm.js APIs. My goal is to allow users to copy strings from the clipboard.
Currently, I have implemented the following code:
this.term.onKey((key) => {
if (key.domEvent.code === 'KeyC'){
if (key.domEvent.ctrlKey) {
this.copiedText = this.term.getSelection();
}
} else if (key.domEvent.code === 'KeyV'){
if (key.domEvent.ctrlKey) {
this.term.write(this.copiedText);
}
}
}
However, this code only allows me to copy text within the terminal and does not detect the command key on MAC when using ctl + c & ctl + v.
By using onData(), I can trigger an event by pressing command + V and see the output outside the terminal:
this.term.onData((data) => {
console.log(data.toString()); // prints "strings I copied with command + c"
});
The issue here is that "data" is just a string triggered by a key press event, causing conflicts with onKey(). I am unsure how to conditionally handle "data" since it is not an object.