why doesn't this code work?

i dont know why, but this code doesn't work.

Imports System.Runtime.InteropServices

PublicClass Form1

Dim MOUSEEVENTF_LEFTDOWNAs UInt32 = &H2

Dim MOUSEEVENTF_LEFTUPAs UInt32 = &H4

Dim MOUSEEVENTF_RIGHTDOWNAs UInt32 = &H8

Dim MOUSEEVENTF_RIGHTUPAs UInt32 = &H10

PrivateSub Button1_Click(ByVal senderAs System.Object,ByVal eAs System.EventArgs)Handles Button1.Click

Dim xAsInteger = 5

Dim yAsInteger = 5

Dim clsNewAsNew Class1

Class1.MouseClick1()

EndSub

EndClass

PublicClass Class1

DeclareAutoFunction mouse_eventLib"user32.dll" (ByVal dwflagsAsInteger,ByVal dxAsInteger,ByVal dyAsInteger,ByVal dwDataAsInteger,ByVal dwExtraInfoAsInteger)AsInteger

Const MOUSEEVENTF_LEFTDOWNAsInteger = 2

Const MOUSEEVENTF_LEFTUPAsInteger = 4

Const MOUSEEVENTF_RIGHTDOWNAsInteger = 8

Const MOUSEEVENTF_RIGHTUPAsInteger = 16

PublicSharedSub MouseClick1()

Dim xAsInteger

Dim yAsInteger

mouse_event(MOUSEEVENTF_LEFTDOWN, x, y, 0, 0)

mouse_event(MOUSEEVENTF_LEFTUP, x, y, 0, 0)

EndSub

EndClass

[5544 byte] By [pavel989] at [2007-12-25]
# 1

I would ask what you trying to achieve or what you expectation is of working ?

Please provide details on what your trying to achieve, that way we can assist you in detailing how to achieve this goal and why the aforementioned code may not be working.

spotty at 2007-8-31 > top of Msdn Tech,Visual Studio Express Editions,Visual Basic 2005 Express Edition...
# 2

Hi,

I tested your code, and as far as I can tell, it does work. Whenever you click the button, the Class1.MouseClick1() sends a new leftmouse click (MOUSEEVENTF_LEFTDOWN & MOUSEVENTF_LEFTUP) to the current loction of the mouse. So as long if you don't move your mouse of the button, it will keep getting click. If you add this code before the Class1.MouseClick1(), you can see that a click event keeps being send to the button, as the color will keep changing from red to blue.

If Button1.BackColor <> Color.Red Then

Button1.BackColor = Color.Red

Else

Button1.BackColor = Color.Blue

End If

Class1.MouseClick1()

If this is not what your are trying to accomplish, please be a little more specific so we can help you further.

Regards

SvenDeBont at 2007-8-31 > top of Msdn Tech,Visual Studio Express Editions,Visual Basic 2005 Express Edition...
# 3
well, thank u very much for that, i guess it does work. but id like to know how to make it click at the X and Y coordinates. oh and just on a side not, can anyone show me how to create a loop that repeats until any key on the keyboard is pressed?
pavel989 at 2007-8-31 > top of Msdn Tech,Visual Studio Express Editions,Visual Basic 2005 Express Edition...
# 4
According to the msdn documentation of the mouse_event function, you'll have to set the MOUSEEVENTF_ABSOLUTE bit of the dwflags parameter to specifiy that the x & y parameter are absolute values. If this is not specified, the x & y parameters are treated as relative to the last know mouse position.

Specifies that the dx and dy parameters contain normalized absolute coordinates. If not set, those parameters contain relative data: the change in position since the last reported position. This flag can be set, or not set, regardless of what kind of mouse or mouse-like device, if any, is connected to the system. For further information about relative mouse motion, see the following Remarks section. See http://msdn.microsoft.com/library/default.asp?url=/library/en-us/winui/winui/windowsuserinterface/userinput/mouseinput/mouseinputreference/mouseinputfunctions/mouse_event.asp for the full documentation

So in your case, the call would look something like this:

Public Const MOUSEEVENTF_ABSOLUTE = &H8000 ' absolute move

mouse_event(MOUSEEVENTF_LEFTDOWN Or MOUSEEVENTF_ABSOLUTE, x, y, 0, 0)

mouse_event(MOUSEEVENTF_LEFTUP Or MOUSEEVENTF_ABSOLUTE, x, y, 0, 0)

This will send a mousedown & up to the top left corner of the sreen.

As for the loop question, do you want to suspend the execution of your application until a key is pressed, or do you need to get notified when a certain key is pressed during normal execution of your app?

Regards,

SvenDeBont at 2007-8-31 > top of Msdn Tech,Visual Studio Express Editions,Visual Basic 2005 Express Edition...
# 5
well i see that it does move it, but what i mean is for to move to a position on the screen once. a specific xy location, unless i did something wrong, the code that u have provided (thnx, btw) keeps moving the xy values.
pavel989 at 2007-8-31 > top of Msdn Tech,Visual Studio Express Editions,Visual Basic 2005 Express Edition...
# 6

I'm not really sure what you mean, but I read the documentation on the mouse_event again, and I think the code I suggested isn't the best and would cause more mousemoves than you intend. A better solution would be to first move the mouse and then perform the clicks. I would look something like this:

'first, move the move to the absolute x,y position
mouse_event(MOUSEEVENTF_MOVE Or MOUSEEVENTF_ABSOLUTE, x, y, 0, 0)

'next, perform the mousedown & mouseup (without moving the mouse)
mouse_event(MOUSEEVENTF_LEFTDOWN, 0, 0, 0, 0)
mouse_event(MOUSEEVENTF_LEFTUP, 0, 0, 0, 0)

Let me know if this worked for you.

Regards,

SvenDeBont at 2007-8-31 > top of Msdn Tech,Visual Studio Express Editions,Visual Basic 2005 Express Edition...
# 7
well it still doesnt move the mouse and i dont really even know if it clicks in the first place.
pavel989 at 2007-8-31 > top of Msdn Tech,Visual Studio Express Editions,Visual Basic 2005 Express Edition...