Capture Key Press

B

Bob Achgill

When I use the code for KeyPress to capture pressing a
certain key for processing on a form with no Text Box it
works.

But when I try the same code on my application that has
text boxes it does does not work.

How can I capture the cursor left and right keys for
processing?

Bob


+++++++++++++++

Private Sub Form1_KeyPress(ByVal sender As Object, ByVal
e As System.Windows.Forms.KeyPressEventArgs) Handles
MyBase.KeyPress
' The keypressed method uses the KeyChar property
to check
' whether the ENTER key is pressed.

' If the ENTER key is pressed, the Handled
property is set to true,
' to indicate the event is handled.

If e.KeyChar = Microsoft.VisualBasic.ChrW(65) Then
e.Handled = True
MsgBox("The following key was depressed " +
ChrW(65)) ' Display message that the " " was pressed
End If

End Sub
 
L

Lloyd Sheen

Set the KeyPreview property on the form to true.

Then form will get all key events and allow you to specify if you have
handled the event. Do this with the handled property on the KeyPress/Key
Down/Key Up events.

Lloyd Sheen
 
B

Bob Achgill

Cool, it works!

Where is the table for all the ChrW(###) codes for
capturing KeyPresses?

I have tried from 1-256 can can't seem to find the cursor
Right/Left/Up/Down indexes.
 
A

Armin Zingler

Bob Achgill said:
When I use the code for KeyPress to capture pressing a
certain key for processing on a form with no Text Box it
works.

But when I try the same code on my application that has
text boxes it does does not work.

How can I capture the cursor left and right keys for
processing?

In addition to Lloyd, this might be of interest:

http://groups.google.com/[email protected]


--
Armin

How to quote and why:
http://www.plig.net/nnq/nquote.html
http://www.netmeister.org/news/learn2quote.html
 
B

Bob Achgill

Thanks! your suggestion lead me to find the "Key
Enumeration" list. In VB .Net doing a search
on "KeyEventArgs.KeyCode Property" and then clicking
on "Keys" will bring up the complete list of keycodes.

Among which are listed:
down Arrow returns 40
up arrow returns 38
left arrow returns 37
right arrow returns 39

I found this code and fixed it to work right to verify
what keycode is returned on pressing down any key.

It is pretty cool.

First make a form and then drag a text box on to it.
Then paste this into the code.

' ======================

Private Sub TextBox1_KeyDown(ByVal sender As Object,
ByVal e As System.Windows.Forms.KeyEventArgs) Handles
TextBox1.KeyDown

TextBox1.Text = "Key pressed was " & e.KeyCode
End Sub
' ======================
 
B

Bob Achgill

Do you see any way using my example keycode finder so I
don't have to put an Keydown event in each text box on my
form?

I tried making a keycode event at the form level but that
did not seem to catch the arrow key when the cursor focus
is on a text box with in the form.
 
B

Bob Achgill

My appologies for my short memory.

I think either you or Loyd gave me this answer already...

Set KeyPreview() = True at the form level to allow
catching arrow or any key downs regardless of where the
cursor focus is on the form

The following code fixes my keyfinder to work anywhere on
the form.
===================
Private Sub Form1_Load(ByVal sender As Object, ByVal e As
System.EventArgs) Handles MyBase.Load
KeyPreview() = True ' Allows catching arrow
key at over all form level using Keydown event

End Sub


===================
 
B

Bob Achgill

OK. Am I missing something. The following code captures
the cursor keys alright on the form for everywhere except
when the focus is on a DataGrid or AxWebBrowser.

Yes i have set the KeyPreview property on the form to
true. Almost home!


' +++++++++++++++++++++++++++++++++++++++++++++++
Private Sub Form1_KeyDown(ByVal sender As Object, ByVal e
As System.Windows.Forms.KeyEventArgs) Handles
MyBase.KeyDown

If e.KeyCode = 37 Then ' Cursor left


If e.KeyCode = 39 Then ' Cursor right

End Sub
' ++++++++++++++++++++++++++++++++++++++++++++++++
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top