Solved

Responding to arrow keys

Posted on 2001-06-13
5
210 Views
Last Modified: 2010-05-02
Hi,

I need to respond to arrow key presses etc while a text box has focus.  There is no problem using the KeyPress, KeyDown events with most keys but they don't seem to fire when the arrow keys are pressed, which seems really odd and is not helpful.  How can I respond to the arrow keys?

Thanks,

Jim
0
Comment
Question by:syntel
5 Comments
 
LVL 4

Accepted Solution

by:
beckingh earned 200 total points
ID: 6187650
You can use the KeyDown event.
0
 
LVL 4

Expert Comment

by:beckingh
ID: 6187662
Private Sub Text1_KeyDown(KeyCode As Integer, Shift As Integer)
Select Case KeyCode
    Case 37
        MsgBox "Left"
    Case 38
        MsgBox "Up"
    Case 39
        MsgBox "Right"
    Case 40
        MsgBox "Down"
End Select

End Sub
0
 
LVL 3

Expert Comment

by:Joebob
ID: 6187791
beckingh is right, but the following has a small modification that makes it a little easier to read.

Private Sub Text1_KeyDown(KeyCode As Integer, Shift As Integer)
  Select Case KeyCode
    Case vbKeyLeft
      MsgBox "Left"
    Case vbKeyUp
      MsgBox "Up"
    Case vbKeyRight
      MsgBox "Right"
    Case vbKeyDown
      MsgBox "Down"
  End Select
End Sub
0
 
LVL 6

Expert Comment

by:sharmon
ID: 6187824
Also, if you need to respond to the arrow keys and the code is not dependent on the textbox that has focus or any other control for that matter.  Put the code in the form_keydown event.  Make sure to turn KeyPreview on for the form in the form's properties collection.  IE: Me.KeyPreview = True via code, or you can do it in the Properties Window for the form.


Private Sub Form_KeyDown(KeyCode As Integer, Shift As Integer)
  Select Case KeyCode
    Case vbKeyLeft
      Debug.Print "Left"
     
    Case vbKeyUp
      Debug.Print "Up"
     
    Case vbKeyRight
      Debug.Print "Right"
     
    Case vbKeyDown
      Debug.Print "Down"
   
    Case Else
   
  End Select
End Sub
0
 

Author Comment

by:syntel
ID: 6187939
I don't know why, I could have sworn the event wasn't firing before when the arrow kay was pressed.  But since it is now I'll just give the first answer the points.

Thanks
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Introduction While answering a recent question (http://www.experts-exchange.com/Q_27402310.html) in the VB classic zone, I wrote some VB code in the (Office) VBA environment, rather than fire up my older PC.  I didn't post completely correct code o…
You can of course define an array to hold data that is of a particular type like an array of Strings to hold customer names or an array of Doubles to hold customer sales, but what do you do if you want to coordinate that data? This article describes…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

910 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

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now