Link to home
Start Free TrialLog in
Avatar of joe37
joe37

asked on

Detect arrow keys in KeyUp or Down or Press events

How can one detect (in Visual Basic 5) when the user presses an arrow key on the keyboard? The KeyUp, KeyDown, KeyPress events seem to be blind to the user pressing those particular keys both for individual controls and for the form (with or without KeyPreview = True.)
Avatar of a111a111a111
a111a111a111

I am sending you the code now
Here is the code:
Private Sub Form_Load()
    KeyPreview = True
End Sub

Private Sub Form_KeyDown(KeyCode As Integer, Shift As Integer)
    Select Case KeyCode
        Case vbKeyLeft: Label1.Caption = "Left"
        Case vbKeyRight: Label1.Caption = "Right"
        Case vbKeyUp: Label1.Caption = "up"
        Case vbKeyDown: Label1.Caption = "Down"
    End Select
End Sub

You do not need the lable1 you can use msgbox or a Flag variable to your application.
Avatar of joe37

ASKER

I want to thank the responder, a111a111a111,  very much.  The answer may be technically correct.  However, the response is essentially what I tried originally. I explicitly stated that I tried KeyPreview = True and still all 3 key events were blind to the arrow keys being pressed (They were NOT blind to other keys being pressed.)  Possibly, this is due to some idiosyncracy in my particular form.  It would be helpful to know if a111a111a111's given code worked in VB 5 for his particular environment or form.  Thanks again.
Avatar of joe37

ASKER

NOTE: I can detect the pressing of the arrow keys if I also press the shift key, but that does me no good - I want to repond to the user just pressing the arrow keys in the usual way.
Intercept the WM_KEYDOWN message for the form -- that'll do it!
Private Sub Form_KeyDown(KeyCode As Integer, Shift As Integer)
Select Case KeyCode
Case vbKeyUp, vbKeyNumpad8
Caption = "You Pressed Up"
Case vbKeyDown, vbKeyNumpad2
Caption = "You Pressed Down"
Case vbKeyRight, vbKeyNumpad6
Caption = "You Pressed Right"
Case vbKeyLeft, vbKeyNumpad4
Caption = "You Pressed Left"
Case vbKeyNumpad8
End Select
End Sub
Oh yes and if there are objects on the form then set the key preview to -1
uhh... Ignore the Second 'Case vbKeyNumpad8'...
Avatar of joe37

ASKER

I had tried that approach before it did not work for me.
Avatar of joe37

ASKER

I may have missed an email - I want to accept VBDesign's answer, but I do NOT see a place to do so here or on any of the hyperlinks from emails that I received.  Perhaps if VBDesigns resubmitted the exact same answer, that would trigger something whereby I could give the credit that is due.
You can't accept vbdesigns answer because he didn't put his suggestion forward as an answer, but as a comment.  If he/she resubmits the same comment as a question then you'll be able to grade it.

pete

P.S. As you've already decided to follow vbdesigns advice i'll not go into detail, but you can also use the GetAsyncKeyState API call to determine if a key is, or has been, pressed since last checked.

Avatar of joe37

ASKER

Thanks to PeterWest for both parts of his reply.  
My answer is correct and it works.
If you need to customize it for your own needs let us know.
Give the points to Me because I answer your question correctly.

Its only points and I worked for that.

Do you need some help with your specific code?

Let me know.

Thank you.

shayplace@hotmail.com
Avatar of joe37

ASKER

Thanks again! As I said in my 1st response, your answer does not work for me!  I tried the equivalent before submitting the question and tried to make that clear. No amount of variation on your theme will work for the situation as described.  I have also stated that I got the answer I was looking for by DBDesign.  He understood the question and its contriants - so did PeterWest.  If either of them resubmits their reponse as an Answer instead of a Comment, I will give them the points. I have stated that offer for VBDesigns previously, but since it appears that he may well not revisit this question, I am also included PeterWest in that offer - as both answered the question asked.    
To: joe37

Can you place the answer here with 0 points.
You know what I mean.
To: joe37

Can you place the answer here with 0 points.

Even as a comment.

Joe37....

I'll have a more detailed look into your problem and post an answer (hopefully) in the next few minutes.

Pete
ASKER CERTIFIED SOLUTION
Avatar of peterwest
peterwest

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of joe37

ASKER

Thanks to PeterWest and VBDesigns - very helpful!