Solved

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

Posted on 1998-11-19
11
353 Views
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"
0
Comment
Question by:groone
  • 3
  • 3
  • 2
  • +3
11 Comments
 
LVL 1

Expert Comment

by:MAVERICK
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)

0
 
LVL 2

Expert Comment

by:trillo
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++
0
 
LVL 2

Expert Comment

by:kswinney
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.

0
Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

 
LVL 3

Author Comment

by:groone
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?

0
 
LVL 15

Expert Comment

by:ameba
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.
0
 
LVL 1

Expert Comment

by:MAVERICK
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


0
 
LVL 15

Expert Comment

by:ameba
ID: 1445756
Hi, MAVERICK
>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.

0
 
LVL 3

Author Comment

by:groone
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


0
 
LVL 3

Accepted Solution

by:
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
    DOWN
    RELEASED
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
        RunLoop
    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
   
    Do
       
        If MouseIsSwapped Then
            'if the mouse has been swapped, look for the right button
            ButtonToCheck = VK_RBUTTON
        Else
            'otherwise, look for the left button
            ButtonToCheck = VK_LBUTTON
        End If
       
        If MouseIsDown Then
            If Not IsKeyPressed(ButtonToCheck) Then CheckForNotePad RELEASED
        Else
            If IsKeyPressed(ButtonToCheck) Then CheckForNotePad DOWN
        End If
       
        DoEvents
        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
        Me.Refresh
    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

0
 
LVL 15

Expert Comment

by:ameba
ID: 1445759
Clever solution without a hook. Great! Congratulations, covington!
0
 
LVL 3

Author Comment

by:groone
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.
0

Featured Post

Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

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.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
VBA error replacing data 6 40
message box in access 4 51
Copy a row 12 64
using web browser with BING 40 131
Introduction While answering a recent question (http://www.experts-exchange.com/Q_27402310.html) in the VB classic zone, I wrote some VB code in the (Office) VBA environment, rather than fire up my older PC.  I didn't post completely correct code o…
Have you ever wanted to restrict the users input in a textbox to numbers, and while doing that make sure that they can't 'cheat' by pasting in non-numeric text? Of course you can do that with code you write yourself but it's tedious and error-prone …
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…
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…

861 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