|
-
Apr 7th, 2009, 09:46 AM
#1
Thread Starter
Member
[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
-
Apr 7th, 2009, 10:01 AM
#2
Re: detecting mouseclick outside the form (for exmple desktop)
Hi,
You can try something like this;
vb Code:
Public Declare Auto Function GetCursorPos Lib "User32.dll" (ByRef lpPoint As Point) As Integer Dim mousepos As Point ' This stores the cordinates of the mouse cursors location Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load Timer1.Enabled = True Timer1.Start() End Sub Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick Dim R As Long = GetCursorPos(mousepos) ' You'll get your location in mousepos Label1.Text = "Mouse Cursor Location : " & mousepos.X & "x" & mousepos.Y End Sub End Class
-
Apr 7th, 2009, 02:46 PM
#3
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:
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:
While True
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.
MsgBox("Mouse click!")
End If
System.Threading.Thread.Sleep(150) 'This can be whatever you want. Lower means it will catch more, but the messagebox might be repeated.
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!
-
Apr 7th, 2009, 04:08 PM
#4
Thread Starter
Member
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 ?
-
Apr 8th, 2009, 09:34 PM
#5
Re: detecting mouseclick outside the form (for exmple desktop)
Oh, whoops! Add
before "As Short".
-
Apr 8th, 2009, 10:59 PM
#6
Thread Starter
Member
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
-
Dec 4th, 2013, 03:46 PM
#7
Addicted Member
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!
-
Dec 12th, 2013, 02:24 PM
#8
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
-
Dec 22nd, 2013, 09:48 AM
#9
Addicted Member
Re: [RESOLVED] detecting mouseclick outside the form (for exmple desktop)
 Originally Posted by minitech
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!
-
Jun 18th, 2014, 08:34 AM
#10
New Member
Re: detecting mouseclick outside the form (for exmple desktop)
 Originally Posted by minitech
Try this:
Put this at the start of your code (inside the Class, if that's where it is):
vb Code:
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:
While True
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.
MsgBox("Mouse click!")
End If
System.Threading.Thread.Sleep(150) 'This can be whatever you want. Lower means it will catch more, but the messagebox might be repeated.
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:
If Getasynckeystate(Keys.ControlKey) <> 0 Then Continue While
This will allow you to block the message box by holding down CRTL.
vb Code:
While True
If Getasynckeystate(Keys.ControlKey) <> 0 Then Continue While
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.
MsgBox("Mouse click! " & Getasynckeystate(Keys.ControlKey))
End If
System.Threading.Thread.Sleep(150) 'This can be whatever you want. Lower means it will catch more, but the messagebox might be repeated.
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|