364,111 Members | 2129 Browsing Online
Community for Developers & IT Professionals
Bytes IT Community

Capture Key Press

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


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

"Bob Achgill" <anonymous@discussions.microsoft.com> wrote in message
news:8f3501c43302$c699b5d0$a601280a@phx.gbl...[color=blue]
> 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[/color]


Nov 20 '05 #2

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



[color=blue]
>-----Original Message-----
>Set the KeyPreview property on the form to true.
>
>Then form will get all key events and allow you to[/color]
specify if you have[color=blue]
>handled the event. Do this with the handled property on[/color]
the KeyPress/Key[color=blue]
>Down/Key Up events.
>
>Lloyd Sheen
>
>"Bob Achgill" <anonymous@discussions.microsoft.com>[/color]
wrote in message[color=blue]
>news:8f3501c43302$c699b5d0$a601280a@phx.gbl...[color=green]
>> When I use the code for KeyPress to capture pressing a
>> certain key for processing on a form with no Text Box[/color][/color]
it[color=blue][color=green]
>> 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,[/color][/color]
ByVal[color=blue][color=green]
>> e As System.Windows.Forms.KeyPressEventArgs) Handles
>> MyBase.KeyPress
>> ' The keypressed method uses the KeyChar[/color][/color]
property[color=blue][color=green]
>> 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)[/color][/color]
Then[color=blue][color=green]
>> e.Handled = True
>> MsgBox("The following key was depressed " +
>> ChrW(65)) ' Display message that the " " was pressed
>> End If
>>
>> End Sub[/color]
>
>
>.
>[/color]
Nov 20 '05 #3

Armin Zingler
P: n/a
Armin Zingler
"Bob Achgill" <anonymous@discussions.microsoft.com> schrieb[color=blue]
> 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?[/color]

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

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




[color=blue]
>-----Original Message-----
>"Bob Achgill" <anonymous@discussions.microsoft.com>[/color]
schrieb[color=blue][color=green]
>> When I use the code for KeyPress to capture pressing a
>> certain key for processing on a form with no Text Box[/color][/color]
it[color=blue][color=green]
>> 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?[/color]
>
>In addition to Lloyd, this might be of interest:
>
>http://groups.google.com/groups?selm=40955b3b%240%[/color]
2424800%249b622d9e%40news.freenet.de[color=blue]
>
>
>--
>Armin
>
>How to quote and why:
>http://www.plig.net/nnq/nquote.html
>http://www.netmeister.org/news/learn2quote.html
>
>.
>[/color]
Nov 20 '05 #5

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


[color=blue]
>-----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
>' ======================
>
>
>
>
>[color=green]
>>-----Original Message-----
>>"Bob Achgill" <anonymous@discussions.microsoft.com>[/color]
>schrieb[color=green][color=darkred]
>>> When I use the code for KeyPress to capture pressing a
>>> certain key for processing on a form with no Text Box[/color][/color]
>it[color=green][color=darkred]
>>> works.
>>>
>>> But when I try the same code on my application that[/color][/color][/color]
has[color=blue][color=green][color=darkred]
>>> text boxes it does does not work.
>>>
>>> How can I capture the cursor left and right keys for
>>> processing?[/color]
>>
>>In addition to Lloyd, this might be of interest:
>>
>>http://groups.google.com/groups?selm=40955b3b%240%[/color]
>2424800%249b622d9e%40news.freenet.de[color=green]
>>
>>
>>--
>>Armin
>>
>>How to quote and why:
>>http://www.plig.net/nnq/nquote.html
>>http://www.netmeister.org/news/learn2quote.html
>>
>>.
>>[/color]
>.
>[/color]
Nov 20 '05 #6

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


===================


[color=blue]
>-----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[/color]
my[color=blue]
>form?
>
>I tried making a keycode event at the form level but[/color]
that[color=blue]
>did not seem to catch the arrow key when the cursor[/color]
focus[color=blue]
>is on a text box with in the form.
>
>
>[color=green]
>>-----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
>>' ======================
>>
>>
>>
>>
>>[color=darkred]
>>>-----Original Message-----
>>>"Bob Achgill" <anonymous@discussions.microsoft.com>[/color]
>>schrieb[color=darkred]
>>>> When I use the code for KeyPress to capture pressing[/color][/color][/color]
a[color=blue][color=green][color=darkred]
>>>> certain key for processing on a form with no Text[/color][/color][/color]
Box[color=blue][color=green]
>>it[color=darkred]
>>>> works.
>>>>
>>>> But when I try the same code on my application that[/color][/color]
>has[color=green][color=darkred]
>>>> 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%[/color]
>>2424800%249b622d9e%40news.freenet.de[color=darkred]
>>>
>>>
>>>--
>>>Armin
>>>
>>>How to quote and why:
>>>http://www.plig.net/nnq/nquote.html
>>>http://www.netmeister.org/news/learn2quote.html
>>>
>>>.
>>>[/color]
>>.
>>[/color]
>.
>[/color]
Nov 20 '05 #7

Bob Achgill
P: n/a
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
' ++++++++++++++++++++++++++++++++++++++++++++++++[color=blue]
>-----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[/color]
cursor[color=blue]
>Right/Left/Up/Down indexes.
>
>
>
>[color=green]
>>-----Original Message-----
>>Set the KeyPreview property on the form to true.
>>
>>Then form will get all key events and allow you to[/color]
>specify if you have[color=green]
>>handled the event. Do this with the handled property[/color][/color]
on[color=blue]
>the KeyPress/Key[color=green]
>>Down/Key Up events.
>>
>>Lloyd Sheen
>>
>>"Bob Achgill" <anonymous@discussions.microsoft.com>[/color]
>wrote in message[color=green]
>>news:8f3501c43302$c699b5d0$a601280a@phx.gbl...[color=darkred]
>>> When I use the code for KeyPress to capture pressing a
>>> certain key for processing on a form with no Text Box[/color][/color]
>it[color=green][color=darkred]
>>> works.
>>>
>>> But when I try the same code on my application that[/color][/color][/color]
has[color=blue][color=green][color=darkred]
>>> 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,[/color][/color]
>ByVal[color=green][color=darkred]
>>> e As System.Windows.Forms.KeyPressEventArgs) Handles
>>> MyBase.KeyPress
>>> ' The keypressed method uses the KeyChar[/color][/color]
>property[color=green][color=darkred]
>>> 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)[/color][/color]
>Then[color=green][color=darkred]
>>> e.Handled = True
>>> MsgBox("The following key was depressed "[/color][/color][/color]
+[color=blue][color=green][color=darkred]
>>> ChrW(65)) ' Display message that the " " was pressed
>>> End If
>>>
>>> End Sub[/color]
>>
>>
>>.
>>[/color]
>.
>[/color]
Nov 20 '05 #8

Post your reply

Help answer this question



Didn't find the answer to your Visual Basic .NET question?

You can also browse similar questions: Visual Basic .NET