By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
443,617 Members | 1,896 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 443,617 IT Pros & Developers. It's quick & easy.

keyPress event

P: n/a
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

*** Sent via Developersdex http://www.developersdex.com ***
Sep 13 '07 #1
Share this Question
Share on Google+
4 Replies


P: n/a
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");
--
Hope this helps.
Thanks and Regards.
Manish Bafna.
MCP and MCTS.

"Claudia Fong" wrote:
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

*** Sent via Developersdex http://www.developersdex.com ***
Sep 13 '07 #2

P: n/a
also one more thing you need to set keypreview property of form to true
--
Hope this helps.
Thanks and Regards.
Manish Bafna.
MCP and MCTS.

"Claudia Fong" wrote:
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

*** Sent via Developersdex http://www.developersdex.com ***
Sep 13 '07 #3

P: n/a
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

*** Sent via Developersdex http://www.developersdex.com ***
Sep 17 '07 #4

P: n/a
You have to use KeyUp or KeyDown events

- Timo

Claudia Fong wrote:
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

*** Sent via Developersdex http://www.developersdex.com ***
Sep 17 '07 #5

This discussion thread is closed

Replies have been disabled for this discussion.