keyPress event

  • Thread starter Thread starter Claudia Fong
  • Start date Start date
C

Claudia Fong

I use keypress event to detect the characters users input, but it
doesn't work with Alt tab.. I need to detect the combination Alt-A or
Alt-F1.. how should I do?

The code is below:

private void Form1_KeyPress(object sender, KeyPressEventArgs e)
{
if ((e.KeyCode = Keys.F1 && e.Alt == true))


{
e.Handled = true;
// Open second form here
Form2 frm = new Form2();
frm.Show();

}
}

Cheers!

Claudi
 
Hi,
Try this code:
if (e.Modifiers == Keys.Alt && e.KeyCode == Keys.A)
MessageBox.Show ("Combination of ALt and A pressed");
else if (e.Modifiers == Keys.Alt && e.KeyCode == Keys.F1)
MessageBox.Show("Combination of ALt and F1 pressed");
 
I got this error:


'System.Windows.Forms.KeyPressEventArgs' does not contain a definition
for 'Modifiers' and

'System.Windows.Forms.KeyPressEventArgs' does not contain a definition
for 'KeyCode'


I used your code..

private void Form1_KeyPress(object sender, KeyPressEventArgs e)
{
if (e.Modifiers == Keys.Alt && e.KeyCode == Keys.A)
MessageBox.Show ("Combination of ALt and A pressed");
else if (e.Modifiers == Keys.Alt && e.KeyCode == Keys.F1)
MessageBox.Show("Combination of ALt and F1 pressed");


}


Cheers!

Claudi
 
Back
Top