0

I've tried this one but it's not working.

     if (e.Control && e.KeyCode == Keys.B &&e.KeyCode == Keys.R )
        {
            btn.PerformClick();
        }
Bernard Vander Beken
  • 4,848
  • 5
  • 54
  • 76

2 Answers2

0

One way to make sure that B is pressed and then followed by R would be to have a bool variable that indicates that B was pressed.

Here's a full example:

public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();

        this.KeyPreview = true;
        this.KeyDown += new KeyEventHandler(Form1_KeyDown);
    }

    bool readyForRKey;

    private void Form1_KeyDown(object sender, KeyEventArgs e)
    {
        if (readyForRKey && e.KeyCode == Keys.R)
        {
            btn.PerformClick();
        }

        readyForRKey = (e.KeyData == (Keys.Control | Keys.B));
    }
}

This will work if the user presses (Ctrl+B, R) regardless of whether or they release the Ctrl key before pressing R.

If, on the other hand, you want to make sure that both B and R are pressed at the same time, this answer might help you.

ICloneable
  • 613
  • 3
  • 17
  • @bekimpeci It works for me. Are you sure you're using the appropriate `KeyDown` event? I used `Form.KeyDown` just as an example. Obviously, you need to use it with the target/active control. Or if you want to press the key combination anywhere, you can use `Form.KeyDown` but make sure the `KeyPreview` property is set to true. – ICloneable Jun 08 '20 at 12:31
0

Please try ProcessCmdKey() like this

protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
{
    if(keyData == (Keys.Control | Keys.B|Keys.R))
    {
        btn.PerformClick();
        return true;
    }

    return base.ProcessCmdKey(ref msg, keyData);
} 
AJITH
  • 1,145
  • 7
  • 8