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

Posted on 1998-11-19
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
  • 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

Good luck.

ScreenConnect 6.0 Free Trial

Want empowering updates? You're in the right place! Discover new features in ScreenConnect 6.0, based on partner feedback, to keep you business operating smoothly and optimally (the way it should be). Explore all of the extras and enhancements for yourself!


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 better be the one answerring ;p


Accepted Solution

covington earned 500 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

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
SLMGR Switches Are Not Working On KMS Host 3 93
Using an encrypted  value to decrypt and display contents in vb6 9 52
Macro Excel - Multiple If conditions 2 72
MsgBox 4 61
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…
I’ve seen a number of people looking for examples of how to access web services from VB6.  I’ve been using a test harness I built in VB6 (using many resources I found online) that I use for small projects to work out how to communicate with web serv…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
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…

777 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