Solved

Responding to arrow keys

Posted on 2001-06-13
5
214 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Run code from text file in vb 1 103
vb6 connector to mongodb 2 165
Export Data to Different .csv Files 27 133
VB6 common control 6 sp6 object library not registerd 3 65
The debugging module of the VB 6 IDE can be accessed by way of the Debug menu item. That menu item can normally be found in the IDE's main menu line as shown in this picture.   There is also a companion Debug Toolbar that looks like the followin…
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…
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
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…

752 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