How to move mouse cursor using C#?
Take a look at the Cursor.Position
Property. It should get you started.
private void MoveCursor()
{
// Set the Current cursor, move the cursor's Position,
// and set its clipping rectangle to the form.
this.Cursor = new Cursor(Cursor.Current.Handle);
Cursor.Position = new Point(Cursor.Position.X - 50, Cursor.Position.Y - 50);
Cursor.Clip = new Rectangle(this.Location, this.Size);
}
How to move mouse cursor using C# using while infinite loop?
Ultimately, the answer here is: "don't".
Windows forms are based on a message pump. If you have an event-handler from one message (like "click") that loops forever, it will never get around to processing other messages (like "draw"), so: your app is now unresponsive.
Instead of an infinite loop: use a Timer
, and move the position in the callback. In this case, a System.Windows.Forms.Timer
would be most appropriate.
How to move mouse cursor with joystick?
You are using
float h = horizontalSpeed * Input.GetAxis("Horizontal") * Time.deltaTime;
float v = verticalSpeed * Input.GetAxis("Vertical") * Time.deltaTime;
GUI.DrawTexture(new Rect(h, Screen.height - v, cursorWidth, cursorHeight), cursorImage);
h
and v
will always have very tiny values since multiplied by Time.deltaTime
and Input.GetAxis
afaik returns usually values between 0
and 1
.
These values don't represent an actual cursor position but rather are the change of the position relative to the last frame.
Your cursor will be stuck somewhere in the top-left corner.
Instead you should store the current position and add your values as change to it like
private Vector2 cursorPosition;
private void Start()
{
Cursor.visible = false;
// optional place it in the center on start
cursorPosition = new Vector2(Screen.width/2f, Screen.height/2f);
}
private void OnGUI()
{
// these are not actual positions but the change between last frame and now
float h = horizontalSpeed * Input.GetAxis("Horizontal") * Time.deltaTime;
float v = verticalSpeed * Input.GetAxis("Vertical") * Time.deltaTime;
// add the changes to the actual cursor position
cursorPosition.x += h;
cursorPosition.y += v;
GUI.DrawTexture(new Rect(cursorPosition.x, Screen.height - cursorPosition.y, cursorWidth, cursorHeight), cursorImage);
}
Also note that horizontalSpeed
and verticalSpeed
are in Pixels / Seconds
and you probably want some values bigger then 2
;)
I used 200
and the Up, Down, Left & Right keys.
C# moving the mouse around realistically
I tried the arc calculation method, turned out to be far to complex and, in the end, it didn't look realistic. Straight lines look much more human, as JP suggests in his comment.
This is a function I wrote to calculate a linear mouse movement. Should be pretty self-explanatory. GetCursorPosition() and SetCursorPosition(Point) are wrappers around the win32 functions GetCursorPos and SetCursorPos.
As far as the math goes - technically, this is called Linear Interpolation of a line segment.
public void LinearSmoothMove(Point newPosition, int steps) {
Point start = GetCursorPosition();
PointF iterPoint = start;
// Find the slope of the line segment defined by start and newPosition
PointF slope = new PointF(newPosition.X - start.X, newPosition.Y - start.Y);
// Divide by the number of steps
slope.X = slope.X / steps;
slope.Y = slope.Y / steps;
// Move the mouse to each iterative point.
for (int i = 0; i < steps; i++)
{
iterPoint = new PointF(iterPoint.X + slope.X, iterPoint.Y + slope.Y);
SetCursorPosition(Point.Round(iterPoint));
Thread.Sleep(MouseEventDelayMS);
}
// Move the mouse to the final destination.
SetCursorPosition(newPosition);
}
Related Topics
Do Try/Catch Blocks Hurt Performance When Exceptions Are Not Thrown
Make Https Call Using Httpclient
How to Stop C# Console Applications from Closing Automatically
Can Anonymous Class Implement Interface
How to Calculate Distance Similarity Measure of Given 2 Strings
Assigning Out/Ref Parameters in Moq
How to Load Dll 'Sqlite.Interop.Dll'
How to Configure Socket Connect Timeout
How to Change the Timeout on a .Net Webclient Object
Split String Containing Command-Line Parameters into String[] in C#
Entity Framework Stored Procedure Table Value Parameter
Create an Array or List of All Dates Between Two Dates
How to Bind Wpf Button to a Command in Viewmodelbase
How to Split a String by a Multi-Character Delimiter in C#
How to Parse Huge JSON File as Stream in JSON.Net
Up, Down, Left and Right Arrow Keys Do Not Trigger Keydown Event