I am writing a tiny web app with an ssh terminal(xterm.js) and some codes block cards alongside. Users can execute commands in terminal by clicking the commands button or just simply type in or paste.
I used onData at the beginning, this API allows me to types in or paste but not able to activate specific event, for example Ctrl + d to disconnect.
onKey seems good. I wrote below codes to attach key event, copy works well but paste does not.
term.attachCustomKeyEventHandler(function (e) {
// Ctrl + Shift + C
if (e.ctrlKey && e.shiftKey && (e.keyCode == 3)) {
var copySucceeded = document.execCommand('copy');
console.log('copy succeeded', copySucceeded);
return false;
}
if (e.ctrlKey && e.shiftKey && e.keyCode == 'v') {
var pasteSucceeded = document.execCommand('paste');
console.log('paste succeeded', pasteSucceeded);
return false;
}
});
Here is my question: onKey and onData both listen on keyboard event. When using both function together, the terminal write twice. It there anyway to listen onData only when paste.
term.onKey(function (ev) {
const char = ev;
console.log(ev)
if(ev.key.charCodeAt(0)===4){
//term.dispose();
socket.disconnect();
}
socket.emit('key', ev.key);
});
term.onData(function(data){
console.log(data);
socket.emit('data',data);
});