473,322 Members | 1,690 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,322 software developers and data experts.

keyPress event

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
4 10981
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Darryn Ross | last post by:
Hi, I am trying to catch the KeyPress event on my datagrid but it isn't working... i have also tried registering the handler with the event like this... dgGLBatch.KeyPress += new...
1
by: Rene | last post by:
Hi, I am running is some problems with the KeyPreview and KeyPress events. The KeyPress event is only triggered when there this an focusable control on the form. When all controls are disabled...
4
by: Tom | last post by:
I have a VB.NET user control that I wrote - this control has three or four other controls on it (textbox, combobox, datetime picker, etc). Now, whenever the control is on a form and the user enters...
15
by: Adam J. Schaff | last post by:
I have noticed that if a user closes a form via pressing return (either while the OK button has focus or if AcceptButton is set to OK for the form) then the "ENTER" keypress event fires ON THE...
3
by: Terry Olsen | last post by:
Is there anyway to cause a KeyPress event in a datagrid cell? The only way I get a keypress event is if none of the cells are selected. I was hoping to have the program respond to a certain...
3
by: Fia | last post by:
Hi In Visual Basic 6 I could call keypress with an integer of a choosen key. For example I could call a textbox's keypress with the number 13 (for the enter key). But now in Visual Basic .Net I...
3
by: Nikolay Evseev | last post by:
Hi, I am trying to trace down the Enter key in my Form.KeyPress event handler. The KeyPreview property is set to false, so I'd assume that all key presses should go through my form's KeyPress...
5
by: Henry Jones | last post by:
I am new to C# and wanted to capture the KeyPress for a textbox. I created some code as follows: private void textBox3_KeyPress(object sender, System.EventArgs e) { this.textBox2.Text =...
2
by: Tony Johansson | last post by:
Hello! I have created a Control that consist of a label and a textbox.I have called this class ctlLabelTextbox. public partial class ctlLabelTextbox : UserControl { .... } The class that I...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.