Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

C# Determine when F3 key is pressed

Posted on 2007-04-11
12
Medium Priority
?
1,353 Views
Last Modified: 2008-02-01
Hi guys 'n gals,

I need to determine when a user presses the F3 key, how would I do that? Would I need to add it to the Form KeyPress event?


Cheers!
0
Comment
Question by:Cyber-Drugs
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 6
  • 4
  • 2
12 Comments
 
LVL 11

Accepted Solution

by:
Babycorn-Starfish earned 1200 total points
ID: 18893369
hi,

try this in the KeyDown event

if (e.KeyCode == Keys.F3)
0
 
LVL 4

Author Comment

by:Cyber-Drugs
ID: 18893415
Hi,

Just tried this:

        private void Form1_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.F3)
            {
                MessageBox.Show("F3 pressed");
            }
        }


and it doesn't work...
0
 
LVL 11

Expert Comment

by:Babycorn-Starfish
ID: 18893456
hmmm...

is your form focussed when you press the key?
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 4

Author Comment

by:Cyber-Drugs
ID: 18893546
Is it, I even clicked onto the Form multiple times to make sure.
0
 
LVL 11

Expert Comment

by:Babycorn-Starfish
ID: 18893620
thats odd.

recreatred my project just to make sure i didn't do anything i hadn't mentioned but i just add the event handler via the properties tab on for the form and put in the code above.

Is the event hooked up? Look in the Form.Designer.cs file in the solution explorer, mine is as follows

/// <summary>
        /// Required method for Designer support - do not modify
        /// the contents of this method with the code editor.
        /// </summary>
        private void InitializeComponent()
        {
            this.SuspendLayout();
            //
            // Form1
            //
            this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
            this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
            this.ClientSize = new System.Drawing.Size(292, 266);
            this.Name = "Form1";
            this.Text = "Form1";
            this.KeyDown += new System.Windows.Forms.KeyEventHandler(this.Form1_KeyDown); //<-----this line here
            this.ResumeLayout(false);
        }

if you have this, set a break point in the event handler and see whether you actually enter that method at all.

0
 
LVL 4

Author Comment

by:Cyber-Drugs
ID: 18894119
Just tried this, and it didn't even detect that...

        private void Form1_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.F3)
            {
                MessageBox.Show("F3 pressed");
            }
            else
            {
                MessageBox.Show(e.KeyCode.ToString());
            }
        }


This one has confused me, any ideas of what else I could try?
0
 
LVL 11

Expert Comment

by:Babycorn-Starfish
ID: 18894147
Hi,

Are you handling any other events? or do you still have any code in there from your earlier attempts?
0
 
LVL 11

Expert Comment

by:Babycorn-Starfish
ID: 18894154
also...

if you try creating a new project with aform and add the event handler in this new project and run it.. does that work - just trying to eliminate other causes :)

0
 
LVL 16

Assisted Solution

by:AlexNek
AlexNek earned 800 total points
ID: 18894169
Don't forget to set KeyPreview property for the form to true; When you have form with the controls then focus /and keyboard events/ come to the active control.
0
 
LVL 11

Expert Comment

by:Babycorn-Starfish
ID: 18894193
always something simple !
0
 
LVL 16

Expert Comment

by:AlexNek
ID: 18894291
No, only somitimes :)
0
 
LVL 4

Author Comment

by:Cyber-Drugs
ID: 18896153
That seems ot have done the trick, cheers!
0

Featured Post

Enroll in September's Course of the Month

This month’s featured course covers 16 hours of training in installation, management, and deployment of VMware vSphere virtualization environments. It's free for Premium Members, Team Accounts, and Qualified Experts!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Article by: Ivo
C# And Nullable Types Since 2.0 C# has Nullable(T) Generic Structure. The idea behind is to allow value type objects to have null values just like reference types have. This concerns scenarios where not all data sources have values (like a databa…
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
Have you created a query with information for a calendar? ... and then, abra-cadabra, the calendar is done?! I am going to show you how to make that happen. Visualize your data!  ... really see it To use the code to create a calendar from a q…
In response to a need for security and privacy, and to continue fostering an environment members can turn to for support, solutions, and education, Experts Exchange has created anonymous question capabilities. This new feature is available to our Pr…

670 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question