To capture the KeyDown
event with which you detect if a user has pressed the keybord shortcut, handle the event KeyDown
with this code (where txtInput
is the TextBox
)
this.txtInput.KeyDown += new System.Windows.Forms.KeyEventHandler(this.txtInput_KeyDown);
Now create that mentioned method (txtInput_KeyDown
) and add the following code to handle specific shortcuts
(P.S. to handle the event and create the method in VisualStudio, just double click on the KeyDown
even in the properties)
// Shortcut Alt+I
if (e.Alt && (e.KeyCode == Keys.I))
{
txtInput.Text = @"somedomain\" + txtInput.Text; // Adds the text to the front of the current text
txtInput.SelectionStart = txtInput.Text.Length; // Sets the cursor to the end of the text
}
// Shortcut Ctrl+K
else if (e.Control && (e.KeyCode == Keys.K))
{
txtInput.Text += @"networkpath\"; // Adds the text to the back of the current text
txtInput.SelectionStart = txtInput.Text.Length; // Sets the cursor to the end of the text
}
As described in the comments in the code, this code will handle two different shortcut combinations differently, modify according to your needs.
Additional reading up can be done here regarding the KeyDown event.
The next step that you mentioned was to save the information and load it when the application starts up again, read the SO posts here and here regarding storing and retrieving user settings.
The MSDN entries regarding user settings:
Using Application Settings and User Settings
How To: Write User Settings at Run Time with C#
Using Settings in C#