Solved

How to handle all KeyPress events in a form?

Posted on 2003-10-26
8
1,227 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 

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

[Live Webinar] The Cloud Skills Gap

As Cloud technologies come of age, business leaders grapple with the impact it has on their team's skills and the gap associated with the use of a cloud platform.

Join experts from 451 Research and Concerto Cloud Services on July 27th where we will examine fact and fiction.

Question has a verified solution.

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

There are many ways to remove duplicate entries in an SQL or Access database. Most make you temporarily insert an ID field, make a temp table and copy data back and forth, and/or are slow. Here is an easy way in VB6 using ADO to remove duplicate row…
When designing a form there are several BorderStyles to choose from, all of which can be classified as either 'Fixed' or 'Sizable' and I'd guess that 'Fixed Single' or one of the other fixed types is the most popular choice. I assume it's the most p…
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…
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…
Suggested Courses
Course of the Month7 days, 14 hours left to enroll

617 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