Solved

Responding to arrow keys

Posted on 2001-06-13
5
209 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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Suggested Solutions

When designing a form there are several BorderStyles to choose from, all of which can be classified as either 'Fixed' or 'Sizable' and I'd guess that 'Fixed Single' or one of the other fixed types is the most popular choice. I assume it's the most p…
Background What I'm presenting in this article is the result of 2 conditions in my work area: We have a SQL Server production environment but no development or test environment; andWe have an MS Access front end using tables in SQL Server but we a…
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 Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…

705 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

13 Experts available now in Live!

Get 1:1 Help Now