I want to make a button on that when pressed, the key combination Ctrl + + is pressed and another where Ctrl + - is pressed. How
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.
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:
Created Button Click Event c#
Trigger a keyboard key press event without pressing the key from keyboard