Link to home
Start Free TrialLog in
Avatar of NewAgeNemesis
NewAgeNemesis

asked on

Detect Arrow Keys in KeyPress Event

In a TextBox1_KeyPress event i want to detect of the user presses an arrow key (up, down, left, right). Would i use something like....

If e.KeyChar = ????? Then
msgbox ("Success!")
End if

If so, what would the value of the KeyChar be fore each of the arrow keys??  thanks!
ASKER CERTIFIED SOLUTION
Avatar of Bob Learned
Bob Learned
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of bman9111
bman9111

thelearnedone sorry to impose but what is the difference between keydown and keypress

  Private Sub TextBox1_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles TextBox1.KeyPress
    End Sub

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

    End Sub
u don't have to answer, I was just curious
The KeyPress event will not be triggered when pressing the arrow keys. The KeyPress event only handles printable characters and the return key. What you may do is use the KeyDown event.

Imports System.Windows.Forms

Private Sub tbName_KeyDown(ByVal sender As Object, ByVal e As _  System.Windows.Forms.KeyEventArgs) Handles tbName.KeyDown
Dim Key As Keys

Select Case e.KeyCode()
    Case Keys.Left
        Console.WriteLine("Left Arrow Key Pressed")
    Case Keys.Right
        Console.WriteLine("Right Arrow Key Pressed")
    Case Keys.Up
        Console.WriteLine("Up Arrow Key Pressed")
    Case Key.Down
        Console.WriteLine("Down Arrow Key Pressed")
End Select

End Sub
Certain control keys, such as the arrow keys are not trapped by the Keypress event handler, but they are trapped by the KeyDown event handler.

From the help:
The KeyPress event is not raised by noncharacter keys; however, the noncharacter keys do raise the KeyDown and KeyUp events.

Bob
I was not trying to hijack YOUR QUESTION. I was attempting to give  NewAgeNemesis a full and complete answer to his question. If you look at the time stamp between my answer and the question from  bman9111 you will see it was 2 minutes the time it took me to andwer the question. But please except my apologies if I have offended you in any way.
thelearnedone, not sure if u are refering to me, if so sorry, like I stated before

thelearnedone sorry to impose but what is the difference between keydown and keypress


could of just stated that I needed to open a question, I have unlimited points, from answer other questions too. I just seem this and had to ask...



really not understanding

It is very hard for me to control myself when other people try to hijack my questions, thinking that I can't handle the
response

Sorry for all the anger....
TheLearnedOne,

I didn't realize there was a rule on this site that states that the first person to post a comment to a question OWNS it.

I was under the impresssion that this is a 'collaborative' site, where several 'experts' may provide input to a given question/problem...
Avatar of NewAgeNemesis

ASKER

thanks