5 Replies - 20756 Views - Last Post: 23 January 2010 - 12:26 PM Rate Topic: -----

#1 Guest_NIXZ*


Reputation:

Change The Mouse Position On The Screen...

Post icon  Posted 22 September 2009 - 02:08 PM

A simple example is: lets say that im trying to make a program that allows you to move the mouse only with the arrows (left/right/up/down) how can i change the position of the mouse
on the screen programmatically??? If i wanted when you press a button the mouse to change position to X=0,Y=0 of the hole screen/desktop not the window of the form how can i do it?
(using VB.NET)...

Sub1:
How can i "MouseRightClick" programmatically?

Sub2:
How can i "MouseLeftClick" programmatically?

All I want to do is have the mouse button click. The exact same as if you click the mouse yourself, but do it with code instead of a finger.

Can someone perfect this code plz:
DeclareSub mouse_event Lib "user32" Alias "mouse_event" (ByVal dwFlags AsLong, ByVal dx AsLong, ByVal dy AsLong, ByVal cButtons AsLong, ByVal dwExtraInfo AsLong)

PrivateSub MyMethod()

Windows.Forms.Cursor.Current.Position = New System.Drawing.Point(225, 105)

mouse_event(&H2, 0, 0, 0, 1)

EndSub



From a post:

Quote

This would allow you to set the cursor position and perform a left mouse click. The different mouse click options (right click, left click, etc.) are controlled by the first parameter of the function. In this case &H2 programatically implements a left mouse down at the x-y position 225,105. Other mouse events may be implemented using these:

&H1 is a mouse move
&H2 is left mouse button down
&H4 is left mouse button up
&H8 is right mouse button up
&H10 is right mouse button down
&H20 is middle mouse button down
&H40 is middle mouse button up



Sub3:
How can i press lets say ")" or "q" or "ALT+F4" programmatically?
Using the keyboard this time...

(and i dont forget to say thanks :D )

This post has been edited by NIXZ: 22 September 2009 - 02:11 PM


Is This A Good Question/Topic? 0

Replies To: Change The Mouse Position On The Screen...

#2 crepitus  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 85
  • View blog
  • Posts: 383
  • Joined: 08-September 09

Re: Change The Mouse Position On The Screen...

Posted 22 September 2009 - 02:35 PM

Why not just use windows' accessibility features - mouse keys I think it's called.
Was This Post Helpful? 0
  • +
  • -

#3 1steinar  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 37
  • Joined: 25-November 08

Re: Change The Mouse Position On The Screen...

Posted 23 September 2009 - 08:14 AM

First of all you will need to declare this:
	Private Declare Function GetAsyncKeyState Lib "user32" (ByVal vKey As Long) As Integer
	Dim speed As Short = 10

Then you will need to add an timer to your form and add this code to the timer:
		For i = 1 To 255
			Dim Resultat As Integer = GetAsyncKeyState(i)
			If Resultat = -32767 Then
				Select Case Chr(i)
					Case Is = CChar("%") 'left button
						Cursor.Position = (New Point(MousePosition.X - speed, MousePosition.Y))
					Case Is = CChar("'") 'right button
						Cursor.Position = (New Point(MousePosition.X + speed, MousePosition.Y))
					Case Is = CChar("&") 'up button
						Cursor.Position = (New Point(MousePosition.X, MousePosition.Y - speed))
					Case Is = CChar("(") 'down button
						Cursor.Position = (New Point(MousePosition.X, MousePosition.Y + speed))
				End Select
			End If
		Next

Was This Post Helpful? 1
  • +
  • -

#4 vbnetskywalker  Icon User is offline

  • D.I.C Head

Reputation: 7
  • View blog
  • Posts: 98
  • Joined: 08-June 08

Re: Change The Mouse Position On The Screen...

Posted 23 September 2009 - 11:16 AM

ok ........see what mister "1steinar" told you about changing the pos of the mouse .......

and I just posted this post to tell you to use the class "SendKeys" to use the keyboard programmatically
send Alt + F4
SendKeys.SendWait("%{F4}")




------------
and take a look at this example from msdn
Dim ProcID As Integer
' Start the Calculator application, and store the process id.
ProcID = Shell("CALC.EXE", AppWinStyle.NormalFocus)
' Activate the Calculator application.
AppActivate(ProcID)
' Send the keystrokes to the Calculator application.
My.Computer.Keyboard.SendKeys("22", True)
My.Computer.Keyboard.SendKeys("*", True)
My.Computer.Keyboard.SendKeys("44", True)
My.Computer.Keyboard.SendKeys("=", True)
' The result is 22 * 44 = 968.



hope this helps ........good luck

This post has been edited by vbnetskywalker: 23 September 2009 - 11:17 AM

Was This Post Helpful? 1
  • +
  • -

#5 1steinar  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 37
  • Joined: 25-November 08

Re: Change The Mouse Position On The Screen...

Posted 23 September 2009 - 11:44 AM

I forgot the clicking part :P
Add this to the top:
	Public Declare Sub mouse_event Lib "user32" Alias "mouse_event" (ByVal dwFlags As Integer, ByVal dx As Integer, ByVal dy As Integer, ByVal cButtons As Integer, ByVal dwExtraInfo As Integer)
	Public Const LEFTDOWN As Short = &H2
	Public Const LEFTUP As Short = &H4
	Public Const MIDDLEDOWN As Short = &H20
	Public Const MIDDLEUP As Short = &H40
	Public Const RIGHTDOWN As Short = &H8
	Public Const RIGHTUP As Short = &H10

And this to the select case in the timer:
					Case Is = CChar(" ") 'space
						mouse_event(LEFTDOWN, 0, 0, 0, 0)
						mouse_event(LEFTUP, 0, 0, 0, 0)
					Case Is = CChar(vbCrLf) 'enter
						Cursor.Position = (New Point(0, 0))

Im sorry about that i forget it and my bad english :P
Was This Post Helpful? 0
  • +
  • -

#6 Guest_NIXZ*


Reputation:

Re: Change The Mouse Position On The Screen...

Posted 23 January 2010 - 12:26 PM

Public Const LEFTDOWN As Short = &H2
Public Const LEFTUP As Short = &H4
Public Const MIDDLEDOWN As Short = &H20
Public Const MIDDLEUP As Short = &H40
Public Const RIGHTDOWN As Short = &H8
Public Const RIGHTUP As Short = &H10

What about RIGHTCLICK or RIGHTDOUBLECLICK does those exists?

:P:P:P:P:P:P:P:P:P:P:P:P:P:P:P:P:P:P:P:P:P:P:P:P
man im stupid!!!! Ill just use RIGHTUP and RIGHTDOWN 4 times in a row for a double click. :P :P :P

This post has been edited by NIXZ: 23 January 2010 - 12:52 PM

Was This Post Helpful? 0

Page 1 of 1