Capturing a MouseDown and Mouse Released with a out of focus form

Posted on 1998-11-19
Medium Priority
Last Modified: 2008-03-10
I have a form with a text box in it.  Say for instance Notepad.exe is in focus and my form is behind it.  Everytime the mouse button is pushed in notepad, I would for the form behind it to display a message "Mouse Button Down" as an example, then when the mouse button is released in Notepad I would want the non focused form to display "Mouse Button Released"
Question by:groone
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
  • 3
  • 3
  • 2
  • +3

Expert Comment

ID: 1445750
Use the private Sub  Form_mousedown & form_mouseup.....

BTW you tend to get better answers if you offer more than 10 points... (the average Q goes for around a 100 ... 50 for easy)


Expert Comment

ID: 1445751
As far as I know, you can't.
You should install a MouseHook, and I think it's not possible in VB. You can only retrieve mouse events on your App's forms.
A Hook is a often system defined message queue, you can build a messagehook that traps all messages before going to other windows, but as I said befor, I think you must use lenguajes such as C++

Expert Comment

ID: 1445752
What you're talking about is cross-task subclassing and is not for the faint of heart.

If you're interested, Dan Appleman has some examples of cross-task subclassing in VB (actually adding menu items, etc. to Notepad) using SpyWorks VB.

You can get more information from www.desaware.com.

Good luck.

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!


Author Comment

ID: 1445753
Because Maverick said to raise the stakes ;p  and I added a question so now I expect some work ;p
This is VB 5
The form must be out of focus for it to capture the mouse down and mouse up event.  Is there anything that does the same function as GetKeyState, GetKeyboardState, and GetAsyncKeyboard for the mouse?  I checked DA's win32 api book and couldnt find anything.  

Added question, how do I clear system keyboard buffers?

LVL 15

Expert Comment

ID: 1445754
>now I expect some work
I think good pointer (or link) is better then a work.
kswinney told you the right answer.
I also know one link: to free C-dll that catches the keys.

Expert Comment

ID: 1445755
Raise the stakes.....:)  I disagree with ameba that a link is better than work.... If you put 350 on a question you can expect a fair bit of work......
Another option is to carefully look through the PAQs for ideas ... I think its there somewhere

LVL 15

Expert Comment

ID: 1445756
>I disagree with ameba that a link is better than work

The idea is not: do the work for somebody, but to HELP.

Example (my first answer on EE)
Q: Make the 1st col of ListView a TreeView?
I answered with code to SET_INDENT, exactly what was asked.
I remember I got the code somewhere on the NET. It also contains code to GET_INDENT.
With a little more effort, instead of pasting my code, isn't it better to find and give the "original" source link with example and "full source"? It is also fair to give credits to original authors.


Author Comment

ID: 1445757
Well, the link that was provided to me was a link to purchase a book that I already own.  The book is fantastic, but it does not have or I cannot find what I need in it concerning my question.  The link that was given was worthless to me...sorry kswinney.  And MAVERICK, if I raise it some more...you better be the one answerring ;p


Accepted Solution

covington earned 2000 total points
ID: 1445758
Put this code into your form.

Option Explicit

Private Declare Function GetAsyncKeyState Lib "user32" (ByVal vKey As Long) As Integer

Private Declare Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long)
Private Declare Function SwapMouseButton Lib "user32" (ByVal bSwap As Long) As Long
Private Declare Function GetForegroundWindow Lib "user32" () As Long
Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long
Private Declare Function SetForegroundWindow Lib "user32" (ByVal hwnd As Long) As Long
Private Const VK_LBUTTON = &H1
Private Const VK_RBUTTON = &H2

Dim hWndNotepad As Long
Dim MouseIsDown As Boolean
Dim Loaded As Boolean

Private Enum STATE
End Enum

Private Function MouseIsSwapped() As Boolean

    Dim lOriginalMouseState As Long
    lOriginalMouseState = SwapMouseButton(False)
    If lOriginalMouseState Then
        MouseIsSwapped = True
        SwapMouseButton lOriginalMouseState
    End If
End Function

Private Sub Form_Activate()

    Static Initialized As Boolean
    If Not Initialized Then
        Initialized = True
        SetForegroundWindow hWndNotepad
    End If

End Sub

Private Sub Form_Load()

    hWndNotepad = FindWindow("Notepad", vbNullString)
    If hWndNotepad = 0 Then
        Shell "Notepad.exe", vbNormalFocus
        hWndNotepad = FindWindow("Notepad", vbNullString)
    End If
    Loaded = True
End Sub

Public Sub RunLoop()

    Dim ButtonToCheck As Long
        If MouseIsSwapped Then
            'if the mouse has been swapped, look for the right button
            ButtonToCheck = VK_RBUTTON
            'otherwise, look for the left button
            ButtonToCheck = VK_LBUTTON
        End If
        If MouseIsDown Then
            If Not IsKeyPressed(ButtonToCheck) Then CheckForNotePad RELEASED
            If IsKeyPressed(ButtonToCheck) Then CheckForNotePad DOWN
        End If
        Sleep 25    'this app does not even register 1% of processor time on most machines
                    'even though it's checking 40 times a second for keyboard events
    Loop While Loaded

End Sub

Private Sub CheckForNotePad(MouseState As STATE)

    If hWndNotepad = GetForegroundWindow Then
        Select Case MouseState
            Case DOWN
                MouseIsDown = True
                Text1 = "Mouse Is Down"
            Case Else
                MouseIsDown = False
                Text1 = "Mouse Is Released"
        End Select
    End If

End Sub

Private Sub Form_Unload(Cancel As Integer)
    Loaded = False

End Sub

Private Function IsKeyPressed(Key As Long) As Boolean

    IsKeyPressed = GetAsyncKeyState(Key)
End Function

LVL 15

Expert Comment

ID: 1445759
Clever solution without a hook. Great! Congratulations, covington!

Author Comment

ID: 1445760
Hey, this was a darn good snippet of code.  Excellent!  As you can see I raised the point value.

I will definately be able to work with this code.

Again thanks, and sorry it took so long for me to get back to you.

Featured Post

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

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

There are many ways to remove duplicate entries in an SQL or Access database. Most make you temporarily insert an ID field, make a temp table and copy data back and forth, and/or are slow. Here is an easy way in VB6 using ADO to remove duplicate row…
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…
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…
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

764 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