From 084fc4b599a0b97f7decfac90502bef8a36b595d Mon Sep 17 00:00:00 2001 From: David Lechner Date: Fri, 12 Jun 2020 16:58:05 -0500 Subject: [PATCH] implement CTRL+SHIFT+C to copy from terminal --- src/components/Terminal.tsx | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/src/components/Terminal.tsx b/src/components/Terminal.tsx index 8ba463a2..059ff564 100644 --- a/src/components/Terminal.tsx +++ b/src/components/Terminal.tsx @@ -49,6 +49,25 @@ class Terminal extends React.Component { this.terminalRef = React.createRef(); } + private handleKeyDownEvent = (e: KeyboardEvent): void => { + // implement CTRL+SHIFT+C keyboard shortcut for copying text from terminal + if (e.key === 'C' && e.ctrlKey && e.shiftKey && !e.altKey && !e.metaKey) { + // this would otherwise open up debug console in web browser + e.preventDefault(); + + if ( + document.hasFocus() && + document.activeElement === + this.terminalRef.current?.getElementsByClassName( + 'xterm-helper-textarea', + )[0] && + this.xterm.hasSelection() + ) { + navigator.clipboard.writeText(this.xterm.getSelection()); + } + } + }; + componentDidMount(): void { if (!this.terminalRef.current) { console.error('Missing terminal reference'); @@ -59,9 +78,11 @@ class Terminal extends React.Component { this.subscription = this.props.dataSource?.subscribe({ next: (d) => this.xterm.write(d), }); + window.addEventListener('keydown', this.handleKeyDownEvent); } componentWillUnmount(): void { + window.removeEventListener('keydown', this.handleKeyDownEvent); this.subscription?.unsubscribe(); this.xterm.dispose(); }