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 simulate mouse cursor movement in C++
You will need to gradually progress your mouse a little bit at a time. Consider, for example the following pseudo-code function:
def moveMouse (endX, endY, stepCount, stepDelay):
GetCurrentPosTo(startX, startY);
for step = 1 to stepCount
currX = startX + (endX - startX) * step / stepCount
currY = startY + (endY - startY) * step / stepCount
SetCurrentPosFrom(currX, currY)
DelayFor(stepDelay)
endfor
enddef
This calculates the current position (within the loop) as some fraction of the journey from (startX, startY)
to (endX, endY)
, adjusting for the number of steps you wish to take.
So using a stepCount
of 100 and stepDelay
of ten milliseconds, the mouse cursor would smoothly move over the period of a second.
There could be other possibilities such as moving the cursor at a specific speed rather than taking a specific time, or specifying a minimum speed and maximum time to combine both methods.
I'll leave that as an extra exercise. Suffice to say it would involve the same method of moving the cursor a little at a time rather than just setting its position to the final value immediately.
Programmatically move mouse cursor with adjustable 'speed' of cursor movement
The measurement from your face tracker would create a measurement value from the range [low1, high1]
Your Cursor.Position.X
would take a value between zero to screen width. Lets notate this range as [low2, high2].
You need to assign Cursor.Position.X
such as:
low2 + (value - low1) * (high2 - low2) / (high1 - low1)
The range you define as [low2, high2] will determine the speed of the movement. Shorter range will move the mouse faster.
Move mouse while holding a Key
I think you can use Keyboard.IsKeyDown(Key) method, so you can increase the position of mouse in the direction you want while the key is still down.
For MSDN Reference :
https://msdn.microsoft.com/en-us/library/system.windows.input.keyboard.iskeydown(v=vs.110).aspx
I hope it helps you, good luck !
Moving mouse cursor in Windows Forms
the Form
your code is on has a Cursor
property. That's not what you want to access.
Instead fully qualify the type, as the error indicates:
System.Windows.Forms.Cursor.Position
Related Topics
An Expression Tree May Not Contain a Call or Invocation That Uses Optional Arguments
Set Database Timeout in Entity Framework
Multi Threading C# Application with SQL Server Database Calls
How to Split an Ienumerable into Two by a Boolean Criteria Without Two Queries
Retrieving Files from Directory That Contains Large Amount of Files
Owin's Getexternallogininfoasync Always Returns Null
Record Video of Screen Using .Net Technologies
Using Profiles in Automapper to Map the Same Types with Different Logic
.Net (Dotnet) Wrappers for Opencv
How to Get the Current Date Without the Time
String.Isnullorwhitespace in Linq Expression
How to Know If a Cell Has an Error in the Formula in C#
Count Number of Bits in a 64-Bit (Long, Big) Integer
C#: Triggering an Event When an Object Is Added to a Queue