By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
432,650 Members | 1,778 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 432,650 IT Pros & Developers. It's quick & easy.

Capture Key Press

P: n/a
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
Nov 20 '05 #1
Share this Question
Share on Google+
7 Replies


P: n/a
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

"Bob Achgill" <an*******@discussions.microsoft.com> wrote in message
news:8f****************************@phx.gbl...
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

Nov 20 '05 #2

P: n/a
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.

-----Original Message-----
Set the KeyPreview property on the form to true.

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

Lloyd Sheen

"Bob Achgill" <an*******@discussions.microsoft.com> wrote in messagenews:8f****************************@phx.gbl...
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

.

Nov 20 '05 #3

P: n/a
"Bob Achgill" <an*******@discussions.microsoft.com> schrieb
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/groups?selm...ews.freenet.de
--
Armin

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

Nov 20 '05 #4

P: n/a
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
' ======================


-----Original Message-----
"Bob Achgill" <an*******@discussions.microsoft.com> schrieb
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/groups?selm=40955b3b%240%

2424800%249b622d9e%40news.freenet.de

--
Armin

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

.

Nov 20 '05 #5

P: n/a
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.
-----Original Message-----
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
' ======================


-----Original Message-----
"Bob Achgill" <an*******@discussions.microsoft.com>

schrieb
When I use the code for KeyPress to capture pressing a
certain key for processing on a form with no Text Boxit 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/groups?selm=40955b3b%240%

2424800%249b622d9e%40news.freenet.de


--
Armin

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

.

.

Nov 20 '05 #6

P: n/a
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
===================
-----Original Message-----
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 myform?

I tried making a keycode event at the form level but thatdid not seem to catch the arrow key when the cursor focusis on a text box with in the form.
-----Original Message-----
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
' ======================


-----Original Message-----
"Bob Achgill" <an*******@discussions.microsoft.com>

schrieb
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/groups?selm=40955b3b%240%

2424800%249b622d9e%40news.freenet.de


--
Armin

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

.

.

.

Nov 20 '05 #7

P: n/a
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
' ++++++++++++++++++++++++++++++++++++++++++++++++
-----Original Message-----
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 cursorRight/Left/Up/Down indexes.

-----Original Message-----
Set the KeyPreview property on the form to true.

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

Lloyd Sheen

"Bob Achgill" <an*******@discussions.microsoft.com>

wrote in message
news:8f****************************@phx.gbl...
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 KeyCharproperty 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

.

.

Nov 20 '05 #8

This discussion thread is closed

Replies have been disabled for this discussion.