HOWTO: Simulate Ctrl+C programatically in SSH or Telnet

In some applications written using Rebex SSH Shell or Rebex Telnet that utilize a terminal emulation or virtual terminal, it might be useful to be able to send Ctrl+C sequence programatically. And although this is easy when you know how Ctrl+C actually behaves in Windows, it is definitely not straightforward enough. So this is how you do this:

C #:

terminalControl.SendToServer(new ConsoleKeyInfo('\x3', ConsoleKey.C, false, false, true));

VB.NET:

terminalControl.SendToServer(New ConsoleKeyInfo(Convert.ToChar(3), ConsoleKey.C, False, False, True))

(where terminalControl is an instance of TerminalControl or VirtualTerminal class)

This code creates a ConsoleKeyInfo that simulates a key with ASCII code 3 (ETX special character), console key ā€˜Cā€™ and a pressed Ctrl key. This simulated keystroke is then sent to the server using the SendToServer method.

Update: Are your curious why sending ' x3' equals to sending Ctrl+C? Do you want to know other common control codes? Check the table in following Wikipedia article: wikipedia.org/wiki/ASCII #ASCII _control _characters