Results 1 to 31 of 31

Thread: [RESOLVED] How to Detect Mouse Click Outside of Form?

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Aug 2016
    Posts
    104

    Resolved [RESOLVED] How to Detect Mouse Click Outside of Form?

    Hi,
    How can I determine If a mouse button is clicked, in- or outside the form

  2. #2
    PowerPoster
    Join Date
    Sep 2006
    Location
    Egypt
    Posts
    2,579

    Re: How to Detect Mouse Click Outside of Form?

    The simplest solution is using GetAsyncKeyState with Timer

    vb Code:
    1. Option Explicit
    2.  
    3. Private Declare Function GetAsyncKeyState Lib "user32" (ByVal vKey As Long) As Integer
    4.  
    5. Private Sub Form_Load()
    6.     Timer1.Interval = 250 ' less interval may cause performance issues
    7.     Timer1.Enabled = True
    8. End Sub
    9.  
    10. Private Sub Timer1_Timer()
    11.     ' Checking left mouse button.
    12.     If GetAsyncKeyState(1) = 0 Then
    13.         Me.Caption = "L up"
    14.     Else
    15.         Me.Caption = "L down"
    16.     End If
    17.    
    18. '    ' Checking right mouse button.
    19. '    If GetAsyncKeyState(2) = 0 Then
    20. '        Me.Caption = "R up"
    21. '    Else
    22. '        Me.Caption = "R down"
    23. '    End If
    24. End Sub

    The advanced solution is hooking the mouse https://www.codeproject.com/Articles...e-Mouse-Events



  3. #3

    Thread Starter
    Lively Member
    Join Date
    Aug 2016
    Posts
    104

    Re: How to Detect Mouse Click Outside of Form?

    Quote Originally Posted by 4x2y View Post
    The simplest solution is using GetAsyncKeyState with Timer

    vb Code:
    1. Option Explicit
    2.  
    3. Private Declare Function GetAsyncKeyState Lib "user32" (ByVal vKey As Long) As Integer
    4.  
    5. Private Sub Form_Load()
    6.     Timer1.Interval = 250 ' less interval may cause performance issues
    7.     Timer1.Enabled = True
    8. End Sub
    9.  
    10. Private Sub Timer1_Timer()
    11.     ' Checking left mouse button.
    12.     If GetAsyncKeyState(1) = 0 Then
    13.         Me.Caption = "L up"
    14.     Else
    15.         Me.Caption = "L down"
    16.     End If
    17.    
    18. '    ' Checking right mouse button.
    19. '    If GetAsyncKeyState(2) = 0 Then
    20. '        Me.Caption = "R up"
    21. '    Else
    22. '        Me.Caption = "R down"
    23. '    End If
    24. End Sub

    The advanced solution is hooking the mouse https://www.codeproject.com/Articles...e-Mouse-Events
    Thank you 4x2y But what about inside/outside the form? according to this code only we determine mouse up/down also we can determine left/right click, but determining inside/outside the form is very important for me and I need it ... help!

  4. #4
    Fanatic Member
    Join Date
    Apr 2017
    Posts
    554

    Re: How to Detect Mouse Click Outside of Form?

    You can use getcursorpos api. This will give you (when you use pointapi which you have to use) the X and Y of mouse in screen coord. You compare the X and Y with your Form Left, Top, Right, and Bottom to determine if mouse is inside/outside your form

  5. #5

    Thread Starter
    Lively Member
    Join Date
    Aug 2016
    Posts
    104

    Re: How to Detect Mouse Click Outside of Form?

    Quote Originally Posted by Code Dummy View Post
    You can use getcursorpos api. This will give you (when you use pointapi which you have to use) the X and Y of mouse in screen coord. You compare the X and Y with your Form Left, Top, Right, and Bottom to determine if mouse is inside/outside your form
    But how? I'm begginer in VB!

  6. #6
    Fanatic Member
    Join Date
    Apr 2017
    Posts
    554

    Re: How to Detect Mouse Click Outside of Form?

    I don't have any examples. If I wanted to make a program that detected were the mouse is at any point in time and someone told me to use GetCursorPos I would first Google GetCursorPos and find existing examples on the net then I would take those examples and study them and using the info I got begin to write my program then if I had problems with that program I would come here to VBForums and ask specific questions. I would do all this for you but I really don't have the time nor patience for all that crap so I would probably wind up doing what your doing and ask someone here to give me the code

  7. #7
    PowerPoster
    Join Date
    Sep 2006
    Location
    Egypt
    Posts
    2,579

    Re: How to Detect Mouse Click Outside of Form?

    Above code detect mouse click inside and outside the form but it isn't get mouse position, the next one do
    vb Code:
    1. Option Explicit
    2. Private Type POINTAPI
    3.     X As Long
    4.     Y As Long
    5. End Type
    6.  
    7. Private Declare Function GetCursorPos Lib "user32" (lpPoint As POINTAPI) As Long
    8. Private Declare Function GetAsyncKeyState Lib "user32" (ByVal vKey As Long) As Integer
    9.  
    10. Private Sub Form_Load()
    11.     Timer1.Interval = 250 ' less interval may cause performance issues
    12.     Timer1.Enabled = True
    13. End Sub
    14.  
    15. Private Sub Timer1_Timer()
    16.     Dim pt As POINTAPI
    17.     ' Checking left mouse button.
    18.     If GetAsyncKeyState(1) <> 0 Then
    19.         GetCursorPos pt
    20.         Me.Caption = "L down at x= " & pt.X & "y= " & pt.Y
    21.     End If
    22.    
    23. '    ' Checking right mouse button.
    24. '    If GetAsyncKeyState(2) <> 0 Then
    25. '        GetCursorPos pt
    26. '        Me.Caption = "R down at x= " & pt.X & "y= " & pt.Y
    27. '    End If
    28.  
    29. End Sub



  8. #8
    Fanatic Member
    Join Date
    Apr 2017
    Posts
    554

    Re: How to Detect Mouse Click Outside of Form?

    It appears to me that you could use some code that tells you if you are inside or outside the Form.

    Maybe this

    Code:
    Private Sub Timer1_Timer()
        Dim pt As POINTAPI
        Dim MyApp As String
        
        ' Checking left mouse button.
        If GetAsyncKeyState(1) <> 0 Then
            GetCursorPos pt
            
            If GetActiveWindow = Me.hWnd Then MyApp = "Inside" Else MyApp = "outside"
            
            Me.Caption = MyApp & " Left down at x = " & pt.X & ", y= " & pt.Y
            Exit Sub
        End If
        
        ' Checking right mouse button.
        If GetAsyncKeyState(2) <> 0 Then
            GetCursorPos pt
    
            If GetActiveWindow = Me.hWnd Then MyApp = "Inside" Else MyApp = "outside"
     
           Me.Caption = MyApp & " Right down at x = " & pt.X & ", y= " & pt.Y
        End If
     
    End Sub
    Last edited by Code Dummy; Aug 27th, 2017 at 09:40 PM.

  9. #9
    VB-aholic & Lovin' It LaVolpe's Avatar
    Join Date
    Oct 2007
    Location
    Beside Waldo
    Posts
    19,541

    Re: How to Detect Mouse Click Outside of Form?

    @Code Dummy. I don't think this is a form. I think it is that dropdown picturebox, within a usercontrol, he was asking questions about, several days ago. But Form_Deactivate is what I think you are referring to.

    A timer approach like you & 4x2y mentioned may be good enough? Subclassing is another option. When "dropdown" is shown, set focus to it, subclass & look for WM_KillFocus message, hide and stop subclass when lost focus or other code hides the "dropdown". Subclassing for a novice isn't an easy solution. Made even more difficult, because the "dropdown" contains other controls, per that other thread. So, WM_KillFocus really applies to any part of the project not associated with the picturebox or another app altogether.
    Insomnia is just a byproduct of, "It can't be done"

    Classics Enthusiast? Here's my 1969 Mustang Mach I Fastback. Her sister '67 Coupe has been adopted

    Newbie? Novice? Bored? Spend a few minutes browsing the FAQ section of the forum.
    Read the HitchHiker's Guide to Getting Help on the Forums.
    Here is the list of TAGs you can use to format your posts
    Here are VB6 Help Files online


    {Alpha Image Control} {Memory Leak FAQ} {Unicode Open/Save Dialog} {Resource Image Viewer/Extractor}
    {VB and DPI Tutorial} {Manifest Creator} {UserControl Button Template} {stdPicture Render Usage}

  10. #10
    Fanatic Member
    Join Date
    Apr 2017
    Posts
    554

    Re: How to Detect Mouse Click Outside of Form?

    Quote Originally Posted by LaVolpe View Post
    @Code Dummy. I don't think this is a form. I think it is that dropdown picturebox, within a usercontrol, he was asking questions about, several days ago.
    I think you got the wrong guy

  11. #11
    VB-aholic & Lovin' It LaVolpe's Avatar
    Join Date
    Oct 2007
    Location
    Beside Waldo
    Posts
    19,541

    Re: How to Detect Mouse Click Outside of Form?

    Quote Originally Posted by Code Dummy View Post
    I think you got the wrong guy
    I think you're right. Had to go back and do some searching. If it is a form, then subclassing can still be an option, though a harder one for novices. In this case, WM_ActivateApp could be the message to trap. Your solution would be easier if the short timer interval is good enough.

    Unfortunately, Form_Deactivate & Form_Activate are project related events, not interactive outside of the project.
    Insomnia is just a byproduct of, "It can't be done"

    Classics Enthusiast? Here's my 1969 Mustang Mach I Fastback. Her sister '67 Coupe has been adopted

    Newbie? Novice? Bored? Spend a few minutes browsing the FAQ section of the forum.
    Read the HitchHiker's Guide to Getting Help on the Forums.
    Here is the list of TAGs you can use to format your posts
    Here are VB6 Help Files online


    {Alpha Image Control} {Memory Leak FAQ} {Unicode Open/Save Dialog} {Resource Image Viewer/Extractor}
    {VB and DPI Tutorial} {Manifest Creator} {UserControl Button Template} {stdPicture Render Usage}

  12. #12
    Fanatic Member
    Join Date
    Apr 2017
    Posts
    554

    Re: How to Detect Mouse Click Outside of Form?

    I used GetActiveWindow and it seems to work just fine (see my earlier post)

  13. #13
    PowerPoster
    Join Date
    Sep 2006
    Location
    Egypt
    Posts
    2,579

    Re: How to Detect Mouse Click Outside of Form?

    Why goes that way to know if click inside the form, there is MouseDown event can be used (for the form or any control inside it), so using Timer, GetAsyncKeyState and GetCursorPos should be used to detect clicks outside the form.



  14. #14
    Fanatic Member
    Join Date
    Apr 2017
    Posts
    554

    Re: How to Detect Mouse Click Outside of Form?

    OK, I tell you what, you use the MouseDown events and put code in them for the Form and all 100 controls and I'll use one simple API in the Timer sub

  15. #15

    Thread Starter
    Lively Member
    Join Date
    Aug 2016
    Posts
    104

    Re: How to Detect Mouse Click Outside of Form?

    Quote Originally Posted by Code Dummy View Post
    I don't have any examples. If I wanted to make a program that detected were the mouse is at any point in time and someone told me to use GetCursorPos I would first Google GetCursorPos and find existing examples on the net then I would take those examples and study them and using the info I got begin to write my program then if I had problems with that program I would come here to VBForums and ask specific questions. I would do all this for you but I really don't have the time nor patience for all that crap so I would probably wind up doing what your doing and ask someone here to give me the code
    Thank you Code Dummy for your nice advices . . . I will do that as you said step by step

  16. #16

    Thread Starter
    Lively Member
    Join Date
    Aug 2016
    Posts
    104

    Re: How to Detect Mouse Click Outside of Form?

    Thank you dears I think that is what I'm looking for I searched what you posted and got these two Codes (Sorry, these are only a copy/paste from another forum)

    Code:
    Private Declare Function GetActiveWindow Lib "user32" () As Long
    
    Private Sub Timer1_Timer()
        If GetActiveWindow <> 0 Then
            'if GetActiveWindow is non-zero
            Label1.Caption = "I have the focus"
        Else
            'if GetActiveWindow is zero
            Label1.Caption = "Another program has the focus"
        End If
    End Sub
    And the second one is:

    Code:
    Private Declare Function GetActiveWindow Lib "user32" () As Long
       Private Declare Function GetWindowTextA Lib "user32" (ByVal hwnd _
          As Long, ByVal lpString As String, ByVal aint As Long) As Long
       Private Declare Function GetForegroundWindow Lib "user32" () As Long
     
    
    Private Sub Command1_Click()
    Dim myhwnd As Long
       Dim s As String
       Dim iret As Long
       Dim nullhwnd As Long
    
       s = Space(256)
    
       myhwnd = GetActiveWindow()
       iret = GetWindowTextA(myhwnd, s, 256)
       Debug.Print myhwnd, s
    
       Shell "Calc.exe", 1
    
       ' Wait for CALC.EXE to launch under Windows NT:
       DoEvents
    
       nullhwnd = GetActiveWindow()
       myhwnd = GetForegroundWindow()
    
       s = Space(256)
       iret = GetWindowTextA(nullhwnd, s, 256)
       Debug.Print nullhwnd, s
    
       s = Space(256)
       iret = GetWindowTextA(myhwnd, s, 256)
       Debug.Print myhwnd, s
    
    End Sub

  17. #17
    PowerPoster Elroy's Avatar
    Join Date
    Jun 2014
    Location
    Near Nashville TN
    Posts
    9,853

    Re: How to Detect Mouse Click Outside of Form?

    Mahmood,

    The reason I didn't reply earlier as I didn't feel you were adequately clear on what you wanted. Here are some of the different scenarios I could envision:

    • Are you talking about clicking on another form within the same application?
    • If not the same application, do we need to detect clicks on the desktop?
    • Is it some specific other application about which we're worried about clicks?
    • What are we trying to do once we detect a click that has occurred somewhere else?
    • Are we truly talking about a "click" or are we talking about a mouse-down-and-drag?
    • If we're talking about mouse-down, then the whole issue of "who has captured the mouse" comes into play.
    • Why wasn't the actual task of what you're trying to do outlined for us?


    There are some system wide mouse hooks floating around. But they're certainly not highly advised. Again, I'd be willing to brainstorm if you're willing to outline what you're trying to do in more detail.

    Best Regards,
    Elroy
    Any software I post in these forums written by me is provided "AS IS" without warranty of any kind, expressed or implied, and permission is hereby granted, free of charge and without restriction, to any person obtaining a copy. To all, peace and happiness.

  18. #18

    Thread Starter
    Lively Member
    Join Date
    Aug 2016
    Posts
    104

    Re: How to Detect Mouse Click Outside of Form?

    Quote Originally Posted by Elroy View Post
    Mahmood,

    The reason I didn't reply earlier as I didn't feel you were adequately clear on what you wanted. Here are some of the different scenarios I could envision:

    • Are you talking about clicking on another form within the same application?
    • If not the same application, do we need to detect clicks on the desktop?
    • Is it some specific other application about which we're worried about clicks?
    • What are we trying to do once we detect a click that has occurred somewhere else?
    • Are we truly talking about a "click" or are we talking about a mouse-down-and-drag?
    • If we're talking about mouse-down, then the whole issue of "who has captured the mouse" comes into play.
    • Why wasn't the actual task of what you're trying to do outlined for us?


    There are some system wide mouse hooks floating around. But they're certainly not highly advised. Again, I'd be willing to brainstorm if you're willing to outline what you're trying to do in more detail.

    Best Regards,
    Elroy
    Many thanks Elroy

    -No I didn't mean clicking neither on another form within the same application nor another application.
    -Yes, Clicking (Or mouse-down) on desktop (Any where Out-Side the application)
    -No, there is no worried application!
    -I have a custom drop-down menu, I want to hide it , On mouse-down (Or clicking) out-side . . .
    -Clicking or Mouse-down something like that
    -what is that?
    -All are these I mentioned above

    I will be happy with your advises and guidance.


    Warm Regards
    Mahmood

  19. #19
    Fanatic Member
    Join Date
    Apr 2017
    Posts
    554

    Re: How to Detect Mouse Click Outside of Form?

    You do realize, Mahmood, that 4x2y posted for you the simplest way to detect mouse up/down anywhere and returns the screen position of the mouse (post 5) and I added an API to detect inside/outside your Form (post 8). Perhaps you are not interested in those and are looking for something else more complex

  20. #20
    PowerPoster
    Join Date
    Jun 2015
    Posts
    2,224

    Re: How to Detect Mouse Click Outside of Form?

    Seems like a job for WM_ACTIVATEAPP

    or better yet, why not close the menu as soon as it loses focus? isn't this handled automatically by the popup menu call?

    How are you showing the custom popup menu? The built in popup menu automatically hides itself, as long as the calling window has prior focus.
    Last edited by DEXWERX; Aug 28th, 2017 at 03:01 PM.

  21. #21
    PowerPoster
    Join Date
    Sep 2006
    Location
    Egypt
    Posts
    2,579

    Re: How to Detect Mouse Click Outside of Form?

    Quote Originally Posted by Mahmood Khaleel Pira View Post
    -I have a custom drop-down menu, I want to hide it , On mouse-down (Or clicking) out-side . . .
    My code in post #2 should do the job, anyway to be more clear try the following
    Code:
    Option Explicit
    Private Declare Function GetAsyncKeyState Lib "user32" (ByVal vKey As Long) As Integer
     
    Private Sub Form_Load()
        Timer1.Interval = 250 ' less interval may cause performance issues
        Timer1.Enabled = True
    End Sub
     
    Private Sub Timer1_Timer()
    
        ' Checking left mouse button.
        If GetAsyncKeyState(1) <> 0 Then
            ' Call the code to hide your custom drop-down menu
            Timer1.Enabled = False ' we don't need after hiding the menu, don't forget to enable it again when showing the menu.
        End If
        
        ' Checking right mouse button.
        If GetAsyncKeyState(2) <> 0 Then
            ' Call the code to hide your custom drop-down menu
            Timer1.Enabled = False ' we don't need after hiding the menu, don't forget to enable it again when showing the menu.
        End If
     
    End Sub



  22. #22
    Addicted Member
    Join Date
    Aug 2017
    Location
    South Africa (in the middle)
    Posts
    160

    Re: How to Detect Mouse Click Outside of Form?

    This is not my code. I got it from another site, many moons ago. I think this is what you are searching for, but hey, even I can (and still do) make mistakes :
    JJGetMapping.zip
    Programmers are very patient people....while programming....but don't expect them to be patient all the time

  23. #23
    PowerPoster
    Join Date
    Sep 2006
    Location
    Egypt
    Posts
    2,579

    Re: How to Detect Mouse Click Outside of Form?

    Quote Originally Posted by Inside View Post
    This is not my code. I got it from another site, many moons ago. I think this is what you are searching for, but hey, even I can (and still do) make mistakes :
    JJGetMapping.zip
    According to his last post #18, he don't need any information about what is the app clicked, so again code in post #21 is VERY ENOUGH!!



  24. #24
    Addicted Member
    Join Date
    Aug 2017
    Location
    South Africa (in the middle)
    Posts
    160

    Re: How to Detect Mouse Click Outside of Form?

    Quote Originally Posted by 4x2y View Post
    VERY ENOUGH!!
    Who will decide?
    You?
    Programmers are very patient people....while programming....but don't expect them to be patient all the time

  25. #25
    PowerPoster
    Join Date
    Sep 2006
    Location
    Egypt
    Posts
    2,579

    Re: How to Detect Mouse Click Outside of Form?

    Quote Originally Posted by Inside View Post
    Who will decide?
    You?
    No, but he did, he already descried what is exactly he want, just review his answer for each point Elroy asked, and after all codes provided to him, he is still asking for advice, so i have advised him "code in post #21 is VERY ENOUGH!!"

    Quote Originally Posted by Mahmood Khaleel Pira View Post
    -No I didn't mean clicking neither on another form within the same application nor another application.
    -Yes, Clicking (Or mouse-down) on desktop (Any where Out-Side the application)
    -No, there is no worried application!
    -I have a custom drop-down menu, I want to hide it , On mouse-down (Or clicking) out-side . . .
    -Clicking or Mouse-down something like that
    -what is that?
    -All are these I mentioned above

    I will be happy with your advises and guidance.



  26. #26
    Addicted Member
    Join Date
    Aug 2017
    Location
    South Africa (in the middle)
    Posts
    160

    Re: How to Detect Mouse Click Outside of Form?

    Quote Originally Posted by 4x2y View Post
    Any where Out-Side the application
    The code I gave is exactly that.

    The more help a person can get the better. Maybe not only for him, but for all programmers, including me.

    I cannot learn enough. And I believe that's for everyone else too
    Programmers are very patient people....while programming....but don't expect them to be patient all the time

  27. #27
    PowerPoster
    Join Date
    Sep 2006
    Location
    Egypt
    Posts
    2,579

    Re: How to Detect Mouse Click Outside of Form?

    Quote Originally Posted by Inside View Post
    The code I gave is exactly that.
    Yes, but with extra things he don't care about or ask for



  28. #28

    Thread Starter
    Lively Member
    Join Date
    Aug 2016
    Posts
    104

    Re: How to Detect Mouse Click Outside of Form?

    Quote Originally Posted by Code Dummy View Post
    You do realize, Mahmood, that 4x2y posted for you the simplest way to detect mouse up/down anywhere and returns the screen position of the mouse (post 5) and I added an API to detect inside/outside your Form (post 8). Perhaps you are not interested in those and are looking for something else more complex
    No! and Yes

    Many thanks, Code Dummy for your code and 4x2y.
    No, your code is enough for me but because I'm a beginner I interested in different codes Elroy did not understand my question I replied to him I expect he has a different idea which can improve me for next time.

  29. #29
    PowerPoster
    Join Date
    Sep 2006
    Location
    Egypt
    Posts
    2,579

    Re: How to Detect Mouse Click Outside of Form?

    Quote Originally Posted by Mahmood Khaleel Pira View Post
    No! and Yes

    Many thanks, Code Dummy for your code and 4x2y.
    No, your code is enough for me
    Then please mark your thread resolved and rep people helped you

    Quote Originally Posted by Mahmood Khaleel Pira View Post
    but because I'm a beginner I interested in different codes Elroy did not understand my question I replied to him I expect he has a different idea which can improve me for next time.
    See the link in my first reply (post #2)



  30. #30

    Thread Starter
    Lively Member
    Join Date
    Aug 2016
    Posts
    104

    Re: How to Detect Mouse Click Outside of Form?

    Quote Originally Posted by 4x2y View Post
    According to his last post #18, he don't need any information about what is the app clicked, so again code in post #21 is VERY ENOUGH!!
    Exactly right 4x2y I got my anwser I will mark it as Resolved.

    Best Regards
    Mahmood

  31. #31

    Thread Starter
    Lively Member
    Join Date
    Aug 2016
    Posts
    104

    Re: How to Detect Mouse Click Outside of Form?

    Quote Originally Posted by Inside View Post
    The code I gave is exactly that.

    The more help a person can get the better. Maybe not only for him, but for all programmers, including me.

    I cannot learn enough. And I believe that's for everyone else too

    Thank Inside

    That is right more codes will help me and others, Soon I got my answer but I interested in different ideas on this question.

    Best Regards
    Mahmood

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width