Solved

How to handle all KeyPress events in a form?

Posted on 2003-10-26
8
1,202 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
Does Powershell have you tied up in knots?

Managing Active Directory does not always have to be complicated.  If you are spending more time trying instead of doing, then it's time to look at something else. For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why

 

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
 

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

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

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

Suggested Solutions

I’ve seen a number of people looking for examples of how to access web services from VB6.  I’ve been using a test harness I built in VB6 (using many resources I found online) that I use for small projects to work out how to communicate with web serv…
If you have ever used Microsoft Word then you know that it has a good spell checker and it may have occurred to you that the ability to check spelling might be a nice piece of functionality to add to certain applications of yours. Well the code that…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…

770 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