|
-
Jun 29th, 2010, 03:57 PM
#1
Thread Starter
Fanatic Member
[RESOLVED] Global Mouse Hook
Got another problem :/
Code:
Public Declare Function GetAsyncKeyState Lib "user32.dll" (ByVal vKey As Int32) As UShort
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
t = New Threading.Thread(New Threading.ThreadStart(AddressOf threadfunc))
t.Start()
End Sub
Public Sub threadfunc()
While running
Dim pressed As Boolean = KeyStatus(Keys.LButton)
If pressed Then
MessageBox.Show("L mouse button pressed")
End If
End While
End Sub
Public Shared ReadOnly Property KeyStatus(ByVal Key As Keys) As Boolean
Get
If Key = Keys.LButton AndAlso My.Computer.Mouse.ButtonsSwapped Then
Key = Keys.RButton
MessageBox.Show("right button pushed")
ElseIf Key = Keys.RButton AndAlso My.Computer.Mouse.ButtonsSwapped Then
Key = Keys.LButton
MessageBox.Show("Lbutton button pushed")
End If
Return GetAsyncKeyState(Key) And &H8000US
End Get
End Property
Private Sub Form1_FormClosing(ByVal sender As System.Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles MyBase.FormClosing
running = False
t.Join()
End Sub
This was the code
and this is the thread HERE
Ok its working and everything but when i want to perform something when click is noticed it says i cant cross-threads or something can you help me out with that please, thanks.
-
Jun 29th, 2010, 04:04 PM
#2
Frenzied Member
Re: Global Mouse Hook
What exactly are you trying to do when the click is noticed.. If you're trying to change the form's UI somehow you're going to have to take a look at delegates..
Justin
-
Jun 29th, 2010, 04:11 PM
#3
Thread Starter
Fanatic Member
Re: Global Mouse Hook
im trying to get the cursor X and Y value when clicked
Is there a way to make cross-thread calls?
-
Jun 29th, 2010, 04:52 PM
#4
Thread Starter
Fanatic Member
-
Jun 29th, 2010, 06:31 PM
#5
Frenzied Member
Re: Global Mouse Hook
Yes you can make cross thread calls, you use delegates to do that.
I'm not sure how you're going to get the mouse coords if the mouse is clicked outside of the form, prolly somemore api calls.
But to use delegates, you would define a function or sub in your form class first...
vb Code:
Public Function getCoords() as Point
Return new Point(mymousex,mymousey)
End Function
Something like that...
Then at the top of your form class you would define a delegate with the same signature...
vb Code:
Private Delegate Function mouseCoords() as Point
Then in your thread function (the one constantly checking for mouse press)
When you want to call that function you would do:
vb Code:
Dim d as New mouseCoords()
Me.Invoke(d)
I'm not sure if you can call Me.Invoke in the thread function, or if you need to pass a parameter to the thread that holds a reference of the form...
Try that and let me know what happens
Justin
-
Jun 30th, 2010, 01:59 PM
#6
Thread Starter
Fanatic Member
Re: Global Mouse Hook
Ok all them are in place, i have an error,
its here
Code:
Private Sub threadfunc()
While running
Dim Rclick As Boolean = KeyStatus(Keys.RButton)
Dim Lclick As Boolean = KeyStatus(Keys.LButton)
If Lclick Then
Dim d As New mouseCoords <---- this
Me.Invoke(d)
End If
If Rclick Then
End If
End While
End Sub
the error is: Delegate 'Venis_Auto_Clicker.Form1.mouseCoords' requires an 'AddressOf' expression or lambda expression as the only argument to its constructor.
-
Jun 30th, 2010, 02:11 PM
#7
Member
Re: Global Mouse Hook
all delegates need a sub/function to point. If you are using the same code as in the monkofox example replace "Dim d as New mouseCoord" with " Dim d as New mouseCoords(AddresOf getCoords)"
-
Jun 30th, 2010, 03:08 PM
#8
Thread Starter
Fanatic Member
-
Jun 30th, 2010, 03:21 PM
#9
Thread Starter
Fanatic Member
Re: Global Mouse Hook
this freezes the aplication for some reason, some help please
Code:
Private Sub threadfunc()
While running
Dim Rclick As Boolean = KeyStatus(Keys.RButton)
Dim Lclick As Boolean = KeyStatus(Keys.LButton)
If Lclick Then
Dim d As New mouseCoords(AddressOf Getcoords)
Me.Invoke(d)
End If
If Rclick Then
End If
End While
End Sub
Public Function Getcoords() As Point
Return New Point(MousePosition.X.ToString, MousePosition.Y.ToString)
End Function
-
Jun 30th, 2010, 03:57 PM
#10
Frenzied Member
Re: Global Mouse Hook
Sorry about that delegate code, hard to remember all that stuff...
Um, I don't see anything that wrong with your code. Only thing I can think of is this line:
vb Code:
Return New Point(MousePosition.X.ToString, MousePosition.Y.ToString)
Might want to leave the .ToString off, but if you're not getting compiler errors I don't see how that would freeze up the application. Does it freeze up right way? Or only after you have left clicked?
Justin
-
Jun 30th, 2010, 04:01 PM
#11
Thread Starter
Fanatic Member
Re: Global Mouse Hook
it freezes when i click exit button in corner, but i added msgbox to the leftclick to see if it was working and it didn't work.
Code:
Private Sub threadfunc()
While running
Dim Rclick As Boolean = KeyStatus(Keys.RButton)
Dim Lclick As Boolean = KeyStatus(Keys.LButton)
If Lclick Then
Dim d As New mouseCoords(AddressOf Getcoords)
Me.Invoke(d)
End If
If Rclick Then
End If
End While
End Sub
Public Function Getcoords() As Point
Return New Point(MousePosition.X, MousePosition.Y)
MsgBox("worked")
End Function
-
Jun 30th, 2010, 04:05 PM
#12
Frenzied Member
Re: Global Mouse Hook
Does it eventually throw an error after it freezes up?
Justin
-
Jun 30th, 2010, 04:13 PM
#13
Frenzied Member
Re: Global Mouse Hook
put the message box before the return statement.
Also, if you want to use the point that the delegate returns I think you can do:
vb Code:
...
Dim obj as Object = Me.Invoke(d)
Dim p as Point = CType(obj,Point)
' then use p for something
...
Justin
-
Jul 1st, 2010, 04:20 PM
#14
Thread Starter
Fanatic Member
Re: Global Mouse Hook
Ok it still freezes on exit and doesn't get the coordinates. this my code so far and where the msgbox's have worked.
Code:
Private Sub threadfunc()
While running
MsgBox("worked2") -WORKED
Dim Rclick As Boolean = KeyStatus(Keys.RButton)
Dim Lclick As Boolean = KeyStatus(Keys.LButton)
If Lclick Then
MsgBox("worked1") - WORKED
Dim d As New mouseCoords(AddressOf Getcoords)
Dim obj As Object = Me.Invoke(d)
Dim p As Point = CType(obj, Point)
Msgbox(p.tostring) -WORKED
End If
If Rclick Then
End If
End While
End Sub
Public Function Getcoords() As Point
Return New Point(MousePosition.X, MousePosition.Y)
MsgBox("worked") - DIDN'T WORK
End Function
any suggestions? why is it freezing when i click exit button?
-
Jul 1st, 2010, 04:28 PM
#15
Thread Starter
Fanatic Member
Re: Global Mouse Hook
and no it doesn't throw any errors after it freezes up, it also doesn't say (Not responding in the title bar)
-
Jul 1st, 2010, 04:48 PM
#16
Thread Starter
Fanatic Member
Re: Global Mouse Hook
Ok also i need to make it so i can put the coordinates into the text boxes, i get the same error about cross-threading or something
i tried to replicate what you showed me but it didn't work some help please.
Basicly i have X1-15 and Y1-15 of textboxes and when the user clicks i am going to put the X and Y values into the textboxes accordingly. Still the problem in the previous post as well
-
Jul 1st, 2010, 04:58 PM
#17
Frenzied Member
Re: Global Mouse Hook
Change
Code:
Public Function Getcoords() As Point
Return New Point(MousePosition.X, MousePosition.Y)
MsgBox("worked") - DIDN'T WORK
End Function
To
Code:
Public Function Getcoords() As Point
MsgBox("worked") - DIDN'T WORK
Return New Point(MousePosition.X, MousePosition.Y)
End Function
And see if the msgbox works...
justin
-
Jul 1st, 2010, 05:28 PM
#18
Thread Starter
Fanatic Member
Re: Global Mouse Hook
Yes okay that bit works, Now on to the other problems, any idea why it freezes when i click exit button?
Also how do i make textboxes be able to crossthread?
-
Jul 1st, 2010, 05:36 PM
#19
Frenzied Member
Re: Global Mouse Hook
Sounds to me like the Thread is gettng hung up, because t.Join() should wait for the thread to finish, then continue on to let the form close.
I'll have to test the full code tomorrow at work.
Justin
-
Jul 1st, 2010, 05:47 PM
#20
Thread Starter
Fanatic Member
Re: Global Mouse Hook
Is there anything i should put on the formclose? Like thread.stop or anything like that? Mayb i have to do it maunally? I dont know, correct me if im wrong.
-
Jul 1st, 2010, 06:06 PM
#21
Thread Starter
Fanatic Member
Re: Global Mouse Hook
I have fixed it myself for once lol. I changed, T.Join() to T.Abort() on the formclosing event.
Now how can i make the coordinates go into the textboxes? I need to cross thread again, i tried to replicate yours but it didn't work, some help please. Thankyou
-
Jul 2nd, 2010, 07:09 AM
#22
Thread Starter
Fanatic Member
-
Jul 2nd, 2010, 08:09 AM
#23
Frenzied Member
Re: Global Mouse Hook
I think I read somewhere that the Abort() method doesn't always work.
If you want to update textboxes from the thread, you'll have to make another delegate, and another function on the form that updates a certain textbox.
And you would invoke that delegate the same way, but you'll want to pass parameters, so you woud invoke it like so:
vb Code:
Dim updatebox as New UpdateTextBoxes(addressof Me.UpdateTB)
Me.Invoke(updatebox,new object(){"TextBox1","TextBox2",mouse.X.tostring,Mouse.Y.tostring})
Where your delegate and the UpdateTB signatures look like:
vb Code:
Private Delegate Sub UpdateTextBoxes(TB1 as string, TB2 as string, X as string, Y as string)
....
Public Sub UpdateTB(TB1 as string, TB2 as string, x as string, y as string)
Dim txtbox1 as textbox = ctype(me.Controls(TB1),TextBox)
dim txtbox2 as textbox = ctype(me.Controls(TB2),TextBox)
txtbox1.text = x
txtbox2.text = y
End sub
Something like that.
Justin
Last edited by MonkOFox; Jul 2nd, 2010 at 09:30 AM.
-
Jul 2nd, 2010, 08:32 AM
#24
Thread Starter
Fanatic Member
Re: Global Mouse Hook
do i have to make 15 functions then for each text box.. Isn't there like a way i can do them all in one sub or function?
-
Jul 2nd, 2010, 08:47 AM
#25
Frenzied Member
Re: Global Mouse Hook
yeah you would just pass the different names to that one function, you don't have to make different ones.
for example:
vb Code:
For i as integer = 1 to 6
UpdateTB("TextBoxX" + i.toString,"TextBoxY" + i.tostring,"X","Y")
Next
Justin
-
Jul 2nd, 2010, 09:13 AM
#26
Thread Starter
Fanatic Member
Re: Global Mouse Hook
I get an error here
says TB1 Type String cannot be converted to Windows.Forms.Form.Textbox or something
Code:
Public Sub UpdateTB(ByVal TB1 As String, ByVal TB2 As String, ByVal x As String, ByVal y As String)
Dim txtbox1 As TextBox = CType(TB1, TextBox)
Dim txtbox2 As TextBox = CType(TB2, TextBox)
End Sub
-
Jul 2nd, 2010, 09:25 AM
#27
Thread Starter
Fanatic Member
Re: Global Mouse Hook
Also Monk i dont want all the textboxes to update at once, Wont this update them all at once?
Code:
For i as integer = 1 to 15
UpdateTB("TextBoxX" + i.toString,"TextBoxY" + i.tostring,"X","Y")
Next
-
Jul 2nd, 2010, 09:31 AM
#28
Frenzied Member
Re: Global Mouse Hook
I updated the UpdateTB code. should've been:
Ctype(me.controls(TB1),textbox)
Ctype(me.controsl(TB2),textbox)
And for your last post, that was just an example. Wasn't intended to be used directly, just an example of how to update a different pair of textboxes with the same sub routine.
Justin
-
Jul 2nd, 2010, 09:38 AM
#29
Thread Starter
Fanatic Member
Re: Global Mouse Hook
Ok well, i want only X1 And Y1 to update at same time then i have a Integer which tells the program how many coordinates are already in.
How can i do it so when cAmount = 1 X1 and Y1 update then when cAmount = 2 X2 and Y2 Update and so on up to 15?
-
Jul 2nd, 2010, 09:45 AM
#30
Frenzied Member
Re: Global Mouse Hook
Have a variable in your thread function that initially hold the value of the first set of boxes to be updated (1). Now in the while loop, if they pressed the left mouse button, update the textboxes X1, and Y1 and then increment the variable (now it equals 2) and so on...
Justin
-
Jul 2nd, 2010, 09:49 AM
#31
Thread Starter
Fanatic Member
Re: Global Mouse Hook
ok done that part, i get error here when i debug. the error is Object of type 'System.Int32' cannot be converted to type 'System.String'
Code:
Me.Invoke(updatebox, New Object() {"X" & i.ToString, "Y" & i.ToString, MousePosition.X, MousePosition.Y})
-
Jul 2nd, 2010, 11:45 AM
#32
Thread Starter
Fanatic Member
Re: Global Mouse Hook
Can anyone help me fix this problem ive been trying for like 2and half hours now, thanks in advance.
-
Jul 2nd, 2010, 11:45 AM
#33
Frenzied Member
Re: Global Mouse Hook
Add a .ToString() to the MousePosition.X and MousePosition.Y, if you look at the signature of the function, it accepts 4 string, not two strings and two integers.
Justin
-
Jul 2nd, 2010, 11:56 AM
#34
Thread Starter
Fanatic Member
Re: Global Mouse Hook
Thankyou at last!, but... another problem is: Object reference not set to an instance of an object.
That normally means u need to use the New keyword thingy but i dont want a new textbox so what would i do?
Code:
Public Sub UpdateTB(ByVal TB1 As String, ByVal TB2 As String, ByVal x As String, ByVal y As String)
Dim txtbox1 As TextBox = CType(Me.Controls(TB1), TextBox)
Dim txtbox2 As TextBox = CType(Me.Controls(TB2), TextBox)
txtbox1.Text = x <- IT POINTS TO THIS WHEN IT GIVES ERROR AT DEBUG
txtbox2.Text = y <- THIS IS ALSO THE SAME SO IT WILL PROBLY POINT TO THIS AFTER
End Sub
Last edited by Emcrank; Jul 2nd, 2010 at 12:00 PM.
-
Jul 2nd, 2010, 12:04 PM
#35
Frenzied Member
Re: Global Mouse Hook
Make sure when you pass the names of the textboxes to that function, that it matches the name exactly.
for example, if you had a TextBox name TextBox1 and another named TextBox2
You would call the function like so:
vb Code:
Dim one as integer = 1
dim two as integer = 2
UpdateTB("TextBox"+one.tostring,"TextBox"+two.tostring,"x","y")
Has to be case sensitive, you can't pass "textBox" or anything else.
Justin
-
Jul 2nd, 2010, 12:14 PM
#36
Thread Starter
Fanatic Member
Re: Global Mouse Hook
I have it like this and i is declared at the top like so
Code:
Dim i As Integer = 1
,
Code:
Private Sub threadfunc()
CollectingStarted = True
Me.Invoke(updatebox, New Object() {"X" & i.ToString, "Y" & i.ToString, MousePosition.X.ToString, MousePosition.Y.ToString})
Dim obj As Object = Me.Invoke(d)
Dim p As Point = CType(obj, Point)
While running
Dim Rclick As Boolean = KeyStatus(Keys.RButton)
Dim Lclick As Boolean = KeyStatus(Keys.LButton)
If CollectingStarted = True Then
If Lclick Then
If cAmount = 1 Then
i = 1
UpdateTB("X" & i.ToString, "Y" & i.ToString, p.X, p.Y)
Threading.Thread.Sleep(500)
cAmount = 2
ElseIf cAmount = 2 Then
i = 2
UpdateTB("X" & i.ToString, "Y" & i.ToString, p.X, p.Y)
Threading.Thread.Sleep(500)
cAmount = 3
ElseIf cAmount = 3 Then
i = 3
UpdateTB("X" & i.ToString, "Y" & i.ToString, p.X, p.Y)
Threading.Thread.Sleep(500)
cAmount = 4
ElseIf cAmount = 4 Then
i = 4
UpdateTB("X" & i.ToString, "Y" & i.ToString, p.X, p.Y)
Threading.Thread.Sleep(500)
cAmount = 5
ElseIf cAmount = 5 Then
i = 5
UpdateTB("X" & i.ToString, "Y" & i.ToString, p.X, p.Y)
Threading.Thread.Sleep(500)
cAmount = 6
ElseIf cAmount = 6 Then
i = 6
UpdateTB("X" & i.ToString, "Y" & i.ToString, p.X, p.Y)
Threading.Thread.Sleep(500)
cAmount = 7
ElseIf cAmount = 7 Then
i = 7
UpdateTB("X" & i.ToString, "Y" & i.ToString, p.X, p.Y)
Threading.Thread.Sleep(500)
cAmount = 8
ElseIf cAmount = 8 Then
i = 8
UpdateTB("X" & i.ToString, "Y" & i.ToString, p.X, p.Y)
Threading.Thread.Sleep(500)
cAmount = 9
ElseIf cAmount = 9 Then
i = 9
UpdateTB("X" & i.ToString, "Y" & i.ToString, p.X, p.Y)
Threading.Thread.Sleep(500)
cAmount = 10
ElseIf cAmount = 10 Then
i = 10
UpdateTB("X" & i.ToString, "Y" & i.ToString, p.X, p.Y)
Threading.Thread.Sleep(500)
cAmount = 11
ElseIf cAmount = 11 Then
i = 11
UpdateTB("X" & i.ToString, "Y" & i.ToString, p.X, p.Y)
Threading.Thread.Sleep(500)
cAmount = 12
ElseIf cAmount = 12 Then
i = 12
UpdateTB("X" & i.ToString, "Y" & i.ToString, p.X, p.Y)
Threading.Thread.Sleep(500)
cAmount = 13
ElseIf cAmount = 13 Then
i = 13
UpdateTB("X" & i.ToString, "Y" & i.ToString, p.X, p.Y)
Threading.Thread.Sleep(500)
cAmount = 14
ElseIf cAmount = 14 Then
i = 14
UpdateTB("X" & i.ToString, "Y" & i.ToString, p.X, p.Y)
Threading.Thread.Sleep(500)
cAmount = 15
ElseIf cAmount = 15 Then
i = 15
UpdateTB("X" & i.ToString, "Y" & i.ToString, p.X, p.Y)
Threading.Thread.Sleep(500)
cAmount = 16
End If
End If
If Rclick Then
End If
End If
End While
End Sub
Public Sub UpdateTB(ByVal TB1 As String, ByVal TB2 As String, ByVal x As String, ByVal y As String)
Dim txtbox1 As TextBox = CType(Me.Controls(TB1), TextBox)
Dim txtbox2 As TextBox = CType(Me.Controls(TB2), TextBox)
txtbox1.Text = x
txtbox2.Text = y
End Sub
-
Jul 2nd, 2010, 12:21 PM
#37
Thread Starter
Fanatic Member
Re: Global Mouse Hook
Is anything wrong with that? If so how can i correct it. Thanks in advance.
-
Jul 2nd, 2010, 01:47 PM
#38
Member
Re: Global Mouse Hook
¿Your textboxes are named "X1", "Y1" literal?
-
Jul 2nd, 2010, 01:49 PM
#39
Frenzied Member
Re: Global Mouse Hook
Again, p.X and p.Y need to have .ToString added to the end.
Secondly, you're not going to be able to directly call UpdateTB from the thread, you'll need to instantiate a delegate with the addressOf set to UpdateTb, then call Me.Invoke(delegateVariable, New Object(){param1,param2,param3,param4})
That should be it.
Justin
-
Jul 2nd, 2010, 01:58 PM
#40
Thread Starter
Fanatic Member
Re: Global Mouse Hook
I added the .Tostring part to them all, so if i call the Me.Invoke(updatebox, New Object() {"X" & i.ToString, "Y" & i.ToString, MousePosition.X.ToString, MousePosition.Y.ToString}) thingy instead of the actual sub(UpdateTB) it will do the UpdateTB sub?
I have these at the top:
Code:
Private Delegate Sub UpdateTextBoxes(ByVal TB1 As String, ByVal TB2 As String, ByVal X As String, ByVal Y As String)
Dim updatebox As New UpdateTextBoxes(AddressOf Me.UpdateTB)
Last edited by Emcrank; Jul 2nd, 2010 at 02:01 PM.
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
|