Terminal input onKey event?

Is there any way I can get a text when the user starts to type anything in the terminal input?
Basically, I need onKey event listener of active Terminal or any alternative for that.

Hi @vivek9patel!

You could subscribe to the onData event of the terminal, like this:

import { TerminalService } from '@theia/terminal/lib/browser/base/terminal-service';

...

@inject(TerminalService) protected readonly terminalService: TerminalService;

...

terminalService.currentTerminal.onData((data: string) => {
  console.log('received data', data);
});

It’s not the same as an onKey event, but maybe it’s sufficient for your use case?

1 Like

Thanks, it works!

One more thing, is there any way we can get the output stream of any command execution??

One more thing, is there any way we can get the output stream of any command execution??

Maybe there’s a better solution than this, but what you could do is connect to the terminal via a WebSocketConnectionProvider. You could have a closer look at how this is achieved in the terminal widget implementation here: https://github.com/eclipse-theia/theia/blob/master/packages/terminal/src/browser/terminal-widget-impl.ts#L504

@inject(WebSocketConnectionProvider) protected readonly webSocketConnectionProvider: WebSocketConnectionProvider;

...

this.webSocketConnectionProvider.listen({
            path: `${terminalsPath}/${this.terminalId}`,
            onConnection: connection => {
                connection.onNotification('onData', (data: string) => {
                   // the 'data' received here includes both user input and command output
                });
               ...
                connection.listen();               
            }
        }, { reconnecting: false });

so you are saying that connection.onNotification('onData',() => {}), is listening to the data when any user executes the command in terminal, right?

Yes, that should be the case.

Ok i checked it, what you are saying is 100% correct!
But there is one thing, i’m getting the both the input and output stream data from websocket listener (‘onData’)
Can we seprate the both?

Talking about this:

Hmmm, unfortunately I don’t see a way of separating the two.

1 Like