tags:

views:

18

answers:

2

I have the below code where I am expecting that when the user will press the CTRL +R , the program will be trigger

public Form1()
 {
   InitializeComponent();
   button1.KeyPress +=new KeyPressEventHandler(button1_KeyPress);

 }

private void button1_KeyPress(object sender, KeyPressEventArgs e)
{

   if ((e.KeyChar == (char)Keys.ControlKey) && (e.KeyChar == (char)Keys.R))
   {
      MessageBox.Show("hello");
    }
}

But it is not working. Also the code is expected to run invariable of 'r' or 'R' is pressed.

Please help where I am making mistake.

Thanks.

+1  A: 

try this:

private void button1_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.Modifiers == Keys.Control && e.KeyCode == Keys.R)
            {
            }

        }
anishmarokey
But I am encountring as for e.KeyCode == Keys.R part, e.KeyCode value is coming as "LButton | ShiftKey" while for Keys.R it is as Keys.R. And hence the code is not working
priyanka.sarkar_2
A: 

If you want your application to be notified when certain combination of keys are pressed by user like Ctrl+C or Ctrl+f then you need to override ProcessCmdKey() method. You can find more information about this method here

Shekhar