Improve company productivity with a Business Account.Sign Up

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 611
  • Last Modified:

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.)
0
joe37
Asked:
joe37
  • 7
  • 6
  • 3
  • +2
1 Solution
 
a111a111a111Commented:
I am sending you the code now
0
 
a111a111a111Commented:
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

0
 
a111a111a111Commented:
You do not need the lable1 you can use msgbox or a Flag variable to your application.
0
The 14th Annual Expert Award Winners

The results are in! Meet the top members of our 2017 Expert Awards. Congratulations to all who qualified!

 
joe37Author Commented:
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.
0
 
joe37Author Commented:
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.
0
 
VBDesignsCommented:
Intercept the WM_KEYDOWN message for the form -- that'll do it!
0
 
AllenC_JrCommented:
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
0
 
AllenC_JrCommented:
Oh yes and if there are objects on the form then set the key preview to -1
0
 
AllenC_JrCommented:
uhh... Ignore the Second 'Case vbKeyNumpad8'...
0
 
joe37Author Commented:
I had tried that approach before it did not work for me.
0
 
joe37Author Commented:
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.
0
 
peterwestCommented:
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.

0
 
joe37Author Commented:
Thanks to PeterWest for both parts of his reply.  
0
 
a111a111a111Commented:
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
0
 
joe37Author Commented:
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.    
0
 
a111a111a111Commented:
To: joe37

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

Can you place the answer here with 0 points.

Even as a comment.

0
 
peterwestCommented:
Joe37....

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

Pete
0
 
peterwestCommented:
Hi again,

I've been looking into a way of hooking the messages so you can filter out the keypress messages and then write your own routine to handle them - unfortunately it's now 5PM and i've gotta dash home for the evening.

I have, however, found the following KB article that explains the process.  Take a look if you want to - if you're prepared to wait then i'll continue looking into the problem in the morning.

Good luck,

Pete


HOWTO: Build a Windows Message Handler with AddressOf in VB5
Last reviewed: October 10, 1997
Article ID: Q170570  
The information in this article applies to:
Microsoft Visual Basic Professional and Enterprise Editions for Windows, version 5.0
Microsoft Visual Studio 97


SUMMARY
Prior to the release of Visual Basic 5.0, many developers used third-party Windows message-handling tools or developed their own with tools such as Microsoft Visual C++. With the addition of the AddressOf function to Visual Basic 5.0, developers can now create their own Windows message-handling routines within their Visual Basic applications.

For example, when a user right-clicks on a textbox in Windows 95 or Windows NT 4.0, the operating system automatically displays a default context menu. This default behavior occurs before the Visual Basic application fires the MouseUp event. Without the use of a Windows message handler, there is no way to replace the default context menu with a custom built context menu.



MORE INFORMATION
WARNING: Using AddressOf may cause unpredictable results if you don't completely understand the concept of function callbacks. You must understand how the Basic portion of the callback works, and also the code of the DLL into which you are passing your function address. Debugging such interactions is difficult because the program runs in the same process as the development environment. In some cases, systematic debugging may not be possible. See details in the REFERENCES section of this article for more information.

The following sample shows how to build a Windows message handler to trap and discard the right-click message to replace the default context menu with a custom built one:


Create a new Standard EXE project. Form1 is created by default.

Use the Menu Editor to add two menu items to the form:


    - For the first item, set its Caption property to "My Popup," its Name
      property to "mnuPopup," and its Visible property to "False."


    - For the second item, set its Caption property to "My Context," its
      Name property to "mnuPopupContext," and use the arrow keys to indent
      the item to be a submenu of the first item.

Add two CommandButtons and a textbox to the form:


    - For the first CommandButton, set its Name property to "cmdHook" and
      its Caption property to "&Hook."


    - For the second CommandButton, set its Name property to "cmdUnHook"
      and its Caption property to "&UnHook."


    - For the textbox, set its Name property to "txtHook" and its Text
      property to "Right Click On Me!"

Add the following code to the form:


      Private Sub cmdHook_Click()
          Hook
      End Sub

      Private Sub cmdUnHook_Click()
          UnHook
      End Sub

      Private Sub Form_Load()
          gHW = txtHook.hWnd
      End Sub

      Private Sub txtHook_MouseUp(Button As Integer, Shift As Integer, _
                                  X As Single, Y As Single)
          If Button = vbRightButton Then
              PopupMenu mnuPopup
          End If
      End Sub


Add a Module to the project.

Add the following code to the new Module:


    Declare Function CallWindowProc Lib "user32" Alias "CallWindowProcA" _
           (ByVal lpPrevWndFunc As Long, _
            ByVal hWnd As Long, _
            ByVal Msg As Long, _
            ByVal wParam As Long, _
            ByVal lParam As Long) As Long

    Declare Function SetWindowLong Lib "user32" Alias "SetWindowLongA" _
           (ByVal hWnd As Long, _
            ByVal nIndex As Long, _
            ByVal dwNewLong As Long) As Long

    Public Const GWL_WNDPROC = -4

    Public Const WM_RBUTTONUP = &H205

    Global lpPrevWndProc As Long
    Global gHW As Long

    Public Sub Hook()
        lpPrevWndProc = SetWindowLong(gHW, GWL_WNDPROC, _
                                     AddressOf WindowProc)
    End Sub

    Public Sub UnHook()
        Dim lngReturnValue As Long
        lngReturnValue = SetWindowLong(gHW, GWL_WNDPROC, lpPrevWndProc)
    End Sub

    Function WindowProc(ByVal hw As Long, _
                        ByVal uMsg As Long, _
                        ByVal wParam As Long, _
                        ByVal lParam As Long) As Long

        Select Case uMsg
            Case WM_RBUTTONUP
                Form1.PopupMenu Form1.mnuPopup
            Case Else
                WindowProc = CallWindowProc(lpPrevWndProc, hw, _
                                           uMsg, wParam, lParam)
        End Select
    End Function


Save the project and run it. Right-click on the textbox and notice that the default context menu appears before the custom menu. Click on the CommandButton marked "Hook" to enable the Windows message handler. Right-click on the textbox and note that the default context menu no longer appears. Be sure to click on the CommandButton marked "UnHook" before quitting the application. Always disable a custom Windows message handler before the application terminates. Clicking the End button while this sample is running with the Windows message handler still enabled will cause an IPF.

REFERENCES
This as an advanced topic for users experienced with using callbacks, typically C developers. If you are not familiar with the use of callbacks, then the following references may apply:

The Visual Basic 5.0 Programmers Guide to the Win32 API by Dan Appleman, ISBN 1-56276-446-2 published by Ziff-Davis

The Win32 SDK Online Help

(c) Microsoft Corporation 1997, All Rights Reserved. Contributions by David Sceppa, Microsoft Corporation
 


--------------------------------------------------------------------------------

Additional query words: AddressOf
Keywords : vb5all kbhowto
Version : 5.0 97
Platform : NT WINDOWS




Last reviewed: October 10, 1997
) 1998 Microsoft Corporation. All rights reserved. Terms of Use.
 


0
 
joe37Author Commented:
Thanks to PeterWest and VBDesigns - very helpful!
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

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.

  • 7
  • 6
  • 3
  • +2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now