Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How do paste to xterm when I copy something from an oitside source? #1411

Closed
katrina-switch opened this issue Apr 24, 2018 · 7 comments
Closed

Comments

@katrina-switch
Copy link

Details

  • Browser: Chrome
  • OS version: Ubuntu 16.4, Ubuntu Core, Windows 10
  • xterm.js version: 3.1

Sorry, I am a bit new to javascript and xterm.
Just a summary, I have a web project where I intergrate xterm to show what's happening in a Linux gateway. I connect to it by sending a websocket message which contains my login credentials. A .net core service runs on the gateway that receives the websocket message and logs me in the SSH and SFTP service of the gateway. And sends back the result to my browser via websocket. Then I display the results using xterm.write(result);
In the web project I have an attachCustomKeyEventHandler that calls my Copy and Paste functions below..

function Copy() {
if (xterm.hasSelection()) {
var copySucceeded = document.execCommand('copy');
copiedText = "" + xterm.getSelection();
console.log('copy succeeded: ' + copiedText, copySucceeded);
}
else
console.log('no selection to copy!');
}

    function Paste() {
        var pasteSucceeded = document.execCommand('paste');
        commands.push(copiedText);
        copiedText = '';
    }

This works when I copy/paste from within the xterm. If I want to copy from an outside source like notepad, how do I implement this using Shift + Insert or Ctrl + Shift + V?

@mofux
Copy link
Contributor

mofux commented Apr 24, 2018

I think you can't. Browsers don't allow direct access to the clipboard for security reasons. xterm.js itself listens for the 'paste' event on its hidden textarea, so if something gets pasted while xterm is focused this event will be triggered and we are able to read the data that was pasted with that event and will write it to the terminal automatically.

@katrina-switch
Copy link
Author

Thank you for the fast response.
Okay so there's no other way we can implement this like an exposed function that we can override?
We just need this function so we can easy copy and paste long commands or long folder paths.
Just need to know just so I can explain it in my report. Thank you again.

@mofux
Copy link
Contributor

mofux commented Apr 24, 2018

I don't quite understand your use-case. Copy & paste from and to xterm should just work fine without any extra code using CTRL+C and CTRL+V keyboard shortcuts.

@katrina-switch
Copy link
Author

Sorry if I got you confused. My question was to how I can implement the paste when I copy from an outside source.
Copy and paste from within xterm works perfect. But when I copy from an outside source i.e. notepad and then I try to paste it to xterm nothing happens.

@mofux
Copy link
Contributor

mofux commented Apr 24, 2018

What browser are you using? Could you try if it works in Chrome? (we had some troubles with pasting in Firefox a while ago, but that should also be fixed if I recall correctly)

@justinjocewicz
Copy link

justinjocewicz commented Apr 24, 2018

Try shift+insert to paste into the xterm. Or right-click and paste. @katrina-switch

@katrina-switch
Copy link
Author

katrina-switch commented Apr 27, 2018

Hi sorry for the late reply. @justinjocewicz right click + paste doesn't work for me, it's still not implemented in my code.

I finally was able to get to paste the string copied from another application. Thanks!
Initially my copiedText is being populated during the copy event. If copy event was not from inside the terminal, then it will still be undefined or ''.

function mypasteEvent(ev) { ev.stopPropagation(); if (copiedText === undefined || copiedText === '') { copiedText = ev.clipboardData.getData('text/plain'); if (copiedText === undefined || copiedText === '') { copiedText = window.getSelection('Text').toString(); if (copiedText === undefined || copiedText === '') { console.log('clipboard has no data!'); } } } commands.push(copiedText); copiedText = ''; ev.preventDefault(); }

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants