Reputation: 1843
I want to trigger Windows hotkeys using my c# application. For example, if I selected copy button in my Application, i want to trigger the Ctrl - C hotkey. If I selected the run button in my application, I want to trigger the Win - R hotkey.
How can I do that?
Thank you.
Upvotes: 2
Views: 1387
Reputation: 2372
"If I selected the run button in my application, I want to trigger the Win - R hotkey."
In JScript, it's a lot simpler do display the Run dialog box. It is still possible in C#, though. You need a reference to Shell32:
Then add using Shell32;
in your code-behind.
In your button's click event, you can do this:
private void runBtn_Click(object sender, EventArgs e)
{
Shell shell = new Shell();
IShellDispatch sd = (IShellDispatch)shell;
sd.FileRun();
}
And you should see somethin' like this:
"...if I selected copy button in my Application, i want to trigger the Ctrl - C hotkey."
Selman22 mentioned the textbox will lose focus if you click another button. Here's the way around it:
private void copyBtn_Click(object sender, EventArgs e)
{
textBox1.Focus(); // <---- here
SendKeys.Send("^c");
}
Upvotes: 2
Reputation: 101731
You can use SendKeys.Send method
For example in your button click event, to trigger CTRL + C
combination you can use this
SendKeys.Send("^c") // CTRL + C
Note: By the way I wouldn't suggest you to do it in button click event.Probably you are trying to copy some text from your textbox.But when you click your button textbox is losing it's focus and selected text is disappear.So key is sending correctly but you can't copy anything.
Upvotes: 7