why doesn't this code work?
i dont know why, but this code doesn't work.
Imports
System.Runtime.InteropServicesPublic
Class Form1Dim MOUSEEVENTF_LEFTDOWNAs UInt32 = &H2Dim MOUSEEVENTF_LEFTUPAs UInt32 = &H4Dim MOUSEEVENTF_RIGHTDOWNAs UInt32 = &H8Dim MOUSEEVENTF_RIGHTUPAs UInt32 = &H10PrivateSub Button1_Click(ByVal senderAs System.Object,ByVal eAs System.EventArgs)Handles Button1.ClickDim xAsInteger = 5Dim yAsInteger = 5Dim clsNewAsNew Class1Class1.MouseClick1()
EndSubEnd
ClassPublic
Class Class1DeclareAutoFunction mouse_eventLib"user32.dll" (ByVal dwflagsAsInteger,ByVal dxAsInteger,ByVal dyAsInteger,ByVal dwDataAsInteger,ByVal dwExtraInfoAsInteger)AsIntegerConst MOUSEEVENTF_LEFTDOWNAsInteger = 2Const MOUSEEVENTF_LEFTUPAsInteger = 4Const MOUSEEVENTF_RIGHTDOWNAsInteger = 8Const MOUSEEVENTF_RIGHTUPAsInteger = 16PublicSharedSub MouseClick1()Dim xAsIntegerDim yAsIntegermouse_event(MOUSEEVENTF_LEFTDOWN, x, y, 0, 0)
mouse_event(MOUSEEVENTF_LEFTUP, x, y, 0, 0)
EndSubEnd
Class
[5544 byte] By [
pavel989] at [2007-12-25]
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.
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 ThenButton1.BackColor = Color.Red
ElseButton1.BackColor = Color.Blue
End IfClass1.MouseClick1()
If this is not what your are trying to accomplish, please be a little more specific so we can help you further.
Regards
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?
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,
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.
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,