Solved

How to handle all KeyPress events in a form?

Posted on 2003-10-26
8
1,190 Views
Last Modified: 2012-06-21
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.

Craig

0
Comment
Question by:Craig_Sparks
8 Comments
 
LVL 3

Accepted Solution

by:
gafoorgk earned 125 total points
ID: 9625056
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
0
 

Author Comment

by:Craig_Sparks
ID: 9625076
Sorry I didnt post that. VB.NET 2002

0
 
LVL 5

Expert Comment

by:rajaamirapu
ID: 9625114
The same KeyPreview for a form exists in VB.NET 2002 check that.
0
 

Author Comment

by:Craig_Sparks
ID: 9625315
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?

Thanks,

Craig





0
Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

 

Expert Comment

by:bugsmalli
ID: 9629388
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
0
 

Expert Comment

by:bugsmalli
ID: 9629454
mybad. Please ignore my previous post..
Did you try keyDown?
0
 

Author Comment

by:Craig_Sparks
ID: 9629677
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?

0
 

Author Comment

by:Craig_Sparks
ID: 9629694
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?
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Most everyone who has done any programming in VB6 knows that you can do something in code like Debug.Print MyVar and that when the program runs from the IDE, the value of MyVar will be displayed in the Immediate Window. Less well known is Debug.Asse…
I was working on a PowerPoint add-in the other day and a client asked me "can you implement a feature which processes a chart when it's pasted into a slide from another deck?". It got me wondering how to hook into built-in ribbon events in Office.
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

707 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

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now