Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Responding to arrow keys

Posted on 2001-06-13
5
Medium Priority
?
220 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 800 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

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

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

Most everyone who has done any programming in VB6 knows that you can do something in code like Debug.Print MyVar and that when the program runs from the IDE, the value of MyVar will be displayed in the Immediate Window. Less well known is Debug.Asse…
Enums (shorthand for ‘enumerations’) are not often used by programmers but they can be quite valuable when they are.  What are they? An Enum is just a type of variable like a string or an Integer, but in this case one that you create that contains…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
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…

782 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