[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 :)
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
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!
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 ?
Re: detecting mouseclick outside the form (for exmple desktop)
Oh, whoops! Add
before "As Short".
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
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
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
Re: [RESOLVED] detecting mouseclick outside the form (for exmple desktop)
Quote:
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!
Re: detecting mouseclick outside the form (for exmple desktop)
Quote:
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