How to handle all KeyPress events in a form?

I know how to create an event handler for keypress events only when they are with respect to an object in the form itself, like a button or a textbox, etc.

How would I create one for the form itself and disable ALL the default handlers, like tab, return, arrow keys etc?

I've tried adding the handler for:

AddHandler Form1.KeyPress
AddHandler Me.KeyPress
AddHandler MyBase.KeyPress

all without sucess, which just shows how lost I am on this topic and would appreciate any help anyone has to offer. Thanks.


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.

if u r talking about vb6, use the following

Private Sub Form_Load()
  Me.KeyPreview = True
End Sub

Private Sub Form_KeyDown(KeyCode As Integer, Shift As Integer)
  If (KeyCode = vbKeyTab) Or (KeyCode = vbKeyReturn) Then
    KeyCode = 0
  End If
End Sub

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
Craig_SparksAuthor Commented:
Sorry I didnt post that. VB.NET 2002

The same KeyPreview for a form exists in VB.NET 2002 check that.
Big Business Goals? Which KPIs Will Help You

The most successful MSPs rely on metrics – known as key performance indicators (KPIs) – for making informed decisions that help their businesses thrive, rather than just survive. This eBook provides an overview of the most important KPIs used by top MSPs.

Craig_SparksAuthor Commented:
I'm still not getting how to fix it.

I have a single form in the program, Form1

In the load section I put:

Private Sub Form1_Load(ByVal sender ...
Me.KeyPreview = True
End Sub

Then try:

Private Sub Form1_KeyPress(ByVal sender As System.Object, ByVal e As KeyPressEventArgs) Handles MyBase.KeyPress

        Label1.Text = e.KeyChar
        e.Handled = True

    End Sub

But it doesn't catch any of the key presses. They system still does, tabs around the buttons in the form etc. The MyBase part has to be wrong on my part, there is no option for Form1.KeyPress only options for the various buttons and MyBase, thats it.  If I replace MyBase with say TextBox1 it works great. How can I achieve this same functionality for the entire form?

Is there a way to effectivly pull off: Handles Form1.KeyPress?



Try the following:

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Me.KeyPreview = True
    End Sub

    Private Sub Form1_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles MyBase.KeyPress
        Label1.Text = e.KeyChar
        e.Handled = True
    End Sub
mybad. Please ignore my previous post..
Did you try keyDown?
Craig_SparksAuthor Commented:
Yes I have tried KeyDown and KeyPress with the same results.  Even with the KeyPreview = True something is intercepting default key moves, like arrows, tabs, space etc. But when something like a text box has focus in the form then the MyBase does catch the key presses.

Any thoughts?

Craig_SparksAuthor Commented:
Actually the MyBase catches all the KeyDown events save for the special keys that I need actually. I dont need to focus any buttons etc and i can catch what keys are being pressed save for:

Arrow Keys, Tabs, Space, Enter and some others I'm sure ive missed.

How to catch those too?
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 Classic

From novice to tech pro — start learning today.