C# Determine when F3 key is pressed

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!
LVL 4
Cyber-DrugsAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Babycorn-StarfishCommented:
hi,

try this in the KeyDown event

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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Cyber-DrugsAuthor Commented:
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
Babycorn-StarfishCommented:
hmmm...

is your form focussed when you press the key?
0
CompTIA Security+

Learn the essential functions of CompTIA Security+, which establishes the core knowledge required of any cybersecurity role and leads professionals into intermediate-level cybersecurity jobs.

Cyber-DrugsAuthor Commented:
Is it, I even clicked onto the Form multiple times to make sure.
0
Babycorn-StarfishCommented:
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
Cyber-DrugsAuthor Commented:
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
Babycorn-StarfishCommented:
Hi,

Are you handling any other events? or do you still have any code in there from your earlier attempts?
0
Babycorn-StarfishCommented:
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
AlexNekCommented:
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
Babycorn-StarfishCommented:
always something simple !
0
AlexNekCommented:
No, only somitimes :)
0
Cyber-DrugsAuthor Commented:
That seems ot have done the trick, cheers!
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C#

From novice to tech pro — start learning today.