keyPress event

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
 
G

Guest

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");
 
C

Claudia Fong

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top