Process c# winforms button as keystroke?

后端 未结 2 521
情深已故
情深已故 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 14:57

    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.

提交回复
热议问题