I want to make a button on that when pressed, the key combination Ctrl + + is pressed and another where Ctrl + - is pressed. How can I achieve this?
-
1This is a duplicate of: http://stackoverflow.com/questions/400113/best-way-to-implement-keyboard-shortcuts-in-a-windows-forms-application – Vedran May 06 '16 at 06:54
-
Not quite, they want it so that when they press a button on their screen it simulates as if a user was pressing the keyboard – Draken May 06 '16 at 06:55
-
Have a look at these following links: http://stackoverflow.com/questions/15323733/created-button-click-event-c-sharp http://stackoverflow.com/questions/15574850/trigger-a-keyboard-key-press-event-without-pressing-the-key-from-keyboard They should answer your question – Draken May 06 '16 at 06:58
-
1btw, what do you want to achieve here? Unless there is some global hook listening to these key combinations, these keys will be directed to the same application that has the buttons, in which case there is no point in sending the keys; you can directly call the functions instead. – dotNET May 06 '16 at 07:12
2 Answers
Here is a possible solution for what you want:
//Button creation
Button myButton= new Button();
myButton.Click += myButton_Click;
Then in the event myButton_Click();
void myButton_Click(object sender, EventArgs e)
{
SendKeys.Send("^({ADD})")
}
You can then do similar for the - key, check here for the key symbols to use
EDIT
The other variation is to do this instead:
void myButton_Click(object sender, EventArgs e)
{
SendKeys.Send("^({+})")
}
The plus sign (+), caret (^), percent sign (%), tilde (~), and parentheses () have special meanings to SendKeys. To specify one of these characters, enclose it within braces ({}). For example, to specify the plus sign, use "{+}".
Sources:
Trigger a keyboard key press event without pressing the key from keyboard
-
-
According to [Microsoft](https://msdn.microsoft.com/en-us/library/system.windows.forms.sendkeys.send%28v=vs.110%29.aspx), the `{ADD}` refers to the plus key on the num pad and `{SUBTRACT}` is for the minus key on the numpad – Draken May 06 '16 at 07:06
-
1Yes. This is the correct answer. To test it try `if (e.KeyData == (Keys.Control | Keys.Add)) { MessageBox.Show("Control + pressed!"); }` in the `KeyDown` event. – Salah Akbari May 06 '16 at 07:08
-
Updated the code slightly, the other way to call the plus sign (Not the numpad one) is to surround it with `{}`, e.g. `"^({+})"`. Where ^ means `ctrl`and `{+}` means + – Draken May 06 '16 at 07:12
-
1I feel WPF-er here:) But the tag is WF, so you can remove `System.Windows.Forms.` prefix of `SendKeys` (it's normally included in WF), and more importantly, change the `RoutedEvent` to `EventArgs`. – Ivan Stoev May 06 '16 at 07:53
You should create two private functions that perform the actual tasks. Then call these functions in your button's click and ProcessCmdKey events (note that KeyDown
or PreviewKeyDown
will not work properly).
private void btnIncrement_Click(object sender, System.EventArgs e)
{
Increment();
}
protected override bool ProcessCmdKey(Message msg, Keys keyData)
{
if (keyData == (Keys.Ctrl | Keys.Oemplus) {
Increment();
}
return base.ProcessCmdKey(msg, keyData);
}
private void Increment()
{
//Do what needs to be done in this case
}
Same goes for the Ctrl + - keys.

- 33,414
- 24
- 162
- 251
-
-
1Is this shortcut handled by your own application (the one that has the buttons)? If yes, there is no point in using `SendKeys`. If you're simply trying to create a shortcut key for a function of your own application, my method is just the right way of doing it in WinForms. – dotNET May 06 '16 at 07:16
-
@dotNET is correct, it seems odd that you want to do this in your application. Why does your form need to send keys as if the keyboard is being pressed? Can it not call a method directly or is there something else embedded that needs those keys? – Draken May 06 '16 at 07:18