• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 395
  • Last Modified:

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

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"
  • 3
  • 3
  • 2
  • +3
1 Solution
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)

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++
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.

The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

grooneAuthor Commented:
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?

>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.
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

>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.

grooneAuthor Commented:
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

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

Clever solution without a hook. Great! Congratulations, covington!
grooneAuthor Commented:
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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

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