Solved

Responding to arrow keys

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

Enroll in July's Course of the Month

July's Course of the Month is now available! Enroll to learn HTML5 and prepare for certification. It's free for Premium Members, Team Accounts, and Qualified Experts.

Question has a verified solution.

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

Have you ever wanted to restrict the users input in a textbox to numbers, and while doing that make sure that they can't 'cheat' by pasting in non-numeric text? Of course you can do that with code you write yourself but it's tedious and error-prone …
Article by: Martin
Here are a few simple, working, games that you can use as-is or as the basis for your own games. Tic-Tac-Toe This is one of the simplest of all games.   The game allows for a choice of who goes first and keeps track of the number of wins for…
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…
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…
Suggested Courses
Course of the Month9 days, 17 hours left to enroll

623 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