A simple msgbox by pressing keyboard

Dear EE,

I have a simple form with a button named:

btnA

well... if I clicked btnA , it will invoke a function call test()

My Question is.....

How can I make a very simple event.... if I pressed "A" in the KEYBOARD key it will invoke test()

How can I make it??
joely2kAsked:
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.

DabasCommented:
Hi joely2k:
YOu can capture one of the form's key events, such as KeyDown

Dabas
0
joely2kAuthor Commented:
   Private Sub btnCreate_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles btnCreate.KeyDown
        If e.KeyCode = Keys.A Then
            MsgBox("test")
        End If
    End Sub


Hi Thanks it works... but then why when I pressed the second time.. it wont work??
0
DabasCommented:
Try to change the code so that it uses the FORM's KeyDown event.
Probably the second time, the focus was not on the button anymore.

Dabas
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
Cloud Class® Course: CompTIA Healthcare IT Tech

This course will help prep you to earn the CompTIA Healthcare IT Technician certification showing that you have the knowledge and skills needed to succeed in installing, managing, and troubleshooting IT systems in medical and clinical settings.

joely2kAuthor Commented:
I did tried my main form but it cannot...

    Private Sub frmMain_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles MyBase.KeyDown
        If e.KeyCode = Keys.B Then
            MsgBox("Why you hit B")
        End If
    End Sub

Neway actually is better if I can use no button... just a simple keyboard pressed will trigger and event for all the time? possible?
0
joely2kAuthor Commented:
Ok can already thanks you so much
0
DabasCommented:
Try setting the Form's KeyPreview Property to True
0
joely2kAuthor Commented:
what is the keypreview for?
0
DabasCommented:
For a B grade to a 25 point question you can probably find it out yourself using help!

Cheers!
0
joely2kAuthor Commented:
Thanks you alot....

I understand helping is just for points...

enjoy life, see ya
0
DabasCommented:
joely2k:
LOL. I have enough points.
Na, its not "just the points".
Its the principle!


Dabas
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
Visual Basic.NET

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.