Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

268
Views
Only listen onData not onKey when paste with xterm.js

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);
  });
about 4 years ago · Juan Pablo Isaza
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!