Solved

Detect arrow keys in KeyUp or Down or Press events

Posted on 1998-09-27
20
588 Views
Last Modified: 2008-01-16
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
Comment
Question by:joe37
  • 7
  • 6
  • 3
  • +2
20 Comments
 
LVL 3

Expert Comment

by:a111a111a111
ID: 1436890
I am sending you the code now
0
 
LVL 3

Expert Comment

by:a111a111a111
ID: 1436891
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
 
LVL 3

Expert Comment

by:a111a111a111
ID: 1436892
You do not need the lable1 you can use msgbox or a Flag variable to your application.
0
 

Author Comment

by:joe37
ID: 1436893
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
 

Author Comment

by:joe37
ID: 1436894
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
 
LVL 2

Expert Comment

by:VBDesigns
ID: 1436895
Intercept the WM_KEYDOWN message for the form -- that'll do it!
0
 
LVL 2

Expert Comment

by:AllenC_Jr
ID: 1436896
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
 
LVL 2

Expert Comment

by:AllenC_Jr
ID: 1436897
Oh yes and if there are objects on the form then set the key preview to -1
0
 
LVL 2

Expert Comment

by:AllenC_Jr
ID: 1436898
uhh... Ignore the Second 'Case vbKeyNumpad8'...
0
 

Author Comment

by:joe37
ID: 1436899
I had tried that approach before it did not work for me.
0
Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

 

Author Comment

by:joe37
ID: 1436900
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
 
LVL 2

Expert Comment

by:peterwest
ID: 1436901
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
 

Author Comment

by:joe37
ID: 1436902
Thanks to PeterWest for both parts of his reply.  
0
 
LVL 3

Expert Comment

by:a111a111a111
ID: 1436903
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
 

Author Comment

by:joe37
ID: 1436904
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
 
LVL 3

Expert Comment

by:a111a111a111
ID: 1436905
To: joe37

Can you place the answer here with 0 points.
You know what I mean.
0
 
LVL 3

Expert Comment

by:a111a111a111
ID: 1436906
To: joe37

Can you place the answer here with 0 points.

Even as a comment.

0
 
LVL 2

Expert Comment

by:peterwest
ID: 1436907
Joe37....

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

Pete
0
 
LVL 2

Accepted Solution

by:
peterwest earned 100 total points
ID: 1436908
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
 

Author Comment

by:joe37
ID: 1436909
Thanks to PeterWest and VBDesigns - very helpful!
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

Introduction While answering a recent question about filtering a custom class collection, I realized that this could be accomplished with very little code by using the ScriptControl (SC) library.  This article will introduce you to the SC library a…
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…
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…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…

758 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

21 Experts available now in Live!

Get 1:1 Help Now