Process c# winforms button as keystroke?

后端 未结 2 503
情深已故
情深已故 2021-01-14 14:00

I want to make a button on that when pressed, the key combination Ctrl + + is pressed and another where Ctrl + - is pressed. How

2条回答
  •  南笙
    南笙 (楼主)
    2021-01-14 15:05

    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

提交回复
热议问题