Results 1 to 10 of 10

Thread: [RESOLVED] detecting mouseclick outside the form (for exmple desktop)

  1. #1

    Thread Starter
    Member
    Join Date
    Feb 2009
    Posts
    55

    Resolved [RESOLVED] detecting mouseclick outside the form (for exmple desktop)

    hi everyone,

    i've searched the forum for the answer but couldn't find it...

    how can i detect mouseclick outside the form (for example desktop)

    any tip will be appriciated

  2. #2
    PowerPoster sparrow1's Avatar
    Join Date
    May 2005
    Location
    Globetrotter
    Posts
    2,820

    Re: detecting mouseclick outside the form (for exmple desktop)

    Hi,

    You can try something like this;

    vb Code:
    1. Public Declare Auto Function GetCursorPos Lib "User32.dll" (ByRef lpPoint As Point) As Integer
    2.  
    3.     Dim mousepos As Point
    4.     ' This stores the cordinates of the mouse cursors location
    5.  
    6.     Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    7.         Timer1.Enabled = True
    8.         Timer1.Start()
    9.  
    10.     End Sub
    11.  
    12.     Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
    13.         Dim R As Long = GetCursorPos(mousepos) ' You'll get your location in mousepos
    14.         Label1.Text = "Mouse Cursor Location : " & mousepos.X & "x" & mousepos.Y
    15.  
    16.     End Sub
    17. End Class
    Wkr,
    sparrow1

    If I helped you, don't forget to Rate my post. Thank you

    I'm using Visual Studio.Net 2003 and
    2005
    How to learn VB.Net Create setup with VB 2005 Drawing for beginners VB.Net Tutorials GDI+ Tutorials
    Video's for beginners

  3. #3
    Stack Overflow mod​erator
    Join Date
    May 2008
    Location
    British Columbia, Canada
    Posts
    2,824

    Re: detecting mouseclick outside the form (for exmple desktop)

    Try this:

    Put this at the start of your code (inside the Class, if that's where it is):
    vb Code:
    1. Declare Auto Function GetAsyncKeyState(ByVal vKey As Long) As Short

    Then, to grab the mouse click, add this Sub, calling it whatever you want:
    vb Code:
    1. While True
    2.       If GetAsyncKeyState(1) <> 0 Then 'Returns 1 if it is being held down, or sets the flag bit if it's the first time captured.
    3.             MsgBox("Mouse click!")
    4.       End If
    5.       System.Threading.Thread.Sleep(150) 'This can be whatever you want. Lower means it will catch more, but the messagebox might be repeated.
    6. End While

    Note that using this will result in an infinite messagebox until the user decides to use Alt+F4. So don't use a messagebox. Start this sub on a different thread, it will now capture mouse clicks.

    Hope this helps!

  4. #4

    Thread Starter
    Member
    Join Date
    Feb 2009
    Posts
    55

    Re: detecting mouseclick outside the form (for exmple desktop)

    sparrow, thanks for the help, i got the mouse-possition figured out, i need mouseclick, but thanks anyway

    minitech,

    i have an error on the
    Code:
    Declare Auto Function GetAsyncKeyState(ByVal vKey As Long) As Short
    its says "Lib expected" , i assume it needs the DLL file ...

    i searched the form for GetAsyncKeyState but couldn't find it
    can you help me declare it so it wont give me the error ?

  5. #5
    Stack Overflow mod​erator
    Join Date
    May 2008
    Location
    British Columbia, Canada
    Posts
    2,824

    Re: detecting mouseclick outside the form (for exmple desktop)

    Oh, whoops! Add
    Code:
    Lib "user32.dll"
    before "As Short".

  6. #6

    Thread Starter
    Member
    Join Date
    Feb 2009
    Posts
    55

    Re: detecting mouseclick outside the form (for exmple desktop)

    it works !!
    i needed to put it after "GetAsyncKeyState"
    like this

    Code:
    Declare Auto Function GetAsyncKeyState Lib "user32.dll" (ByVal vKey As Long) As Short
    thanks alot,
    resolved

  7. #7
    Addicted Member Miklogak's Avatar
    Join Date
    Jan 2013
    Posts
    203

    Re: [RESOLVED] detecting mouseclick outside the form (for exmple desktop)

    Sorry for Late Reply, But I had to

    @Sharik, I dont know you made that work, because this is the correct declaration and not the one above:
    Code:
    Private Declare Function Getasynckeystate Lib "user32" Alias "GetAsyncKeyState" (ByVal VKEY As Integer) As Short
    Enjoy

    A huge thanks to all the Great Developers and Helpers on vBForums for helping me and many others! Special thanks to Dunfiddlin, Paul, TechnoGome, , JayInThe813, ident for helping me with my projects througout the years. Incl. those i forgot to mention!

  8. #8
    Stack Overflow mod​erator
    Join Date
    May 2008
    Location
    British Columbia, Canada
    Posts
    2,824

    Re: [RESOLVED] detecting mouseclick outside the form (for exmple desktop)

    Sorry about that. FWIW: it should work anyways until you try to actually put something bigger than an Integer in, and Alias has never really been necessary.

    This is what I use now:

    Code:
    <DllImport("user32.dll")>
    Private Function GetAsyncKeyState(vKey As Keys) As Short
    End Function

  9. #9
    Addicted Member Miklogak's Avatar
    Join Date
    Jan 2013
    Posts
    203

    Re: [RESOLVED] detecting mouseclick outside the form (for exmple desktop)

    Quote Originally Posted by minitech View Post
    Sorry about that. FWIW: it should work anyways until you try to actually put something bigger than an Integer in, and Alias has never really been necessary.

    This is what I use now:

    Code:
    <DllImport("user32.dll")>
    Private Function GetAsyncKeyState(vKey As Keys) As Short
    End Function
    @MiniTech, Yep, That works well too! Your method first method seemed as a good Idea too. Good job mate! I used weeks figuring out myself what I was doing wrong without searching online. Untill I found out that the Declaration of the Function was done wrong by myself! Its good to have forums like vBForums!
    Last edited by Miklogak; Dec 22nd, 2013 at 09:52 AM.

    A huge thanks to all the Great Developers and Helpers on vBForums for helping me and many others! Special thanks to Dunfiddlin, Paul, TechnoGome, , JayInThe813, ident for helping me with my projects througout the years. Incl. those i forgot to mention!

  10. #10
    New Member
    Join Date
    Jun 2014
    Posts
    1

    Re: detecting mouseclick outside the form (for exmple desktop)

    Quote Originally Posted by minitech View Post
    Try this:

    Put this at the start of your code (inside the Class, if that's where it is):
    vb Code:
    1. Declare Auto Function GetAsyncKeyState(ByVal vKey As Long) As Short

    Then, to grab the mouse click, add this Sub, calling it whatever you want:
    vb Code:
    1. While True
    2.       If GetAsyncKeyState(1) <> 0 Then 'Returns 1 if it is being held down, or sets the flag bit if it's the first time captured.
    3.             MsgBox("Mouse click!")
    4.       End If
    5.       System.Threading.Thread.Sleep(150) 'This can be whatever you want. Lower means it will catch more, but the messagebox might be repeated.
    6.  
    7. End While

    Note that using this will result in an infinite messagebox until the user decides to use Alt+F4. So don't use a messagebox. Start this sub on a different thread, it will now capture mouse clicks.

    Hope this helps!
    This is exactly what I was looking for thanks. One suggestion on the infinite messagebox though. Add a check for the control key prior to the check for the mouse click to continue the while if CRTL is being held.

    VB Code:
    1. If Getasynckeystate(Keys.ControlKey) <> 0 Then Continue While

    This will allow you to block the message box by holding down CRTL.

    vb Code:
    1. While True
    2.      If Getasynckeystate(Keys.ControlKey) <> 0 Then Continue While
    3.      If Getasynckeystate(1) <> 0 Then 'Returns 1 if it is being held down, or sets the flag bit if it's the first time captured.
    4.           MsgBox("Mouse click! " & Getasynckeystate(Keys.ControlKey))
    5.      End If
    6.      System.Threading.Thread.Sleep(150) 'This can be whatever you want. Lower means it will catch more, but the messagebox might be repeated.
    7.  
    8. End While

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