How can I simulate a mouse click at a certain position on the screen?
Here's a code that is using unmanaged functions to simulate mouse clicks :
//This is a replacement for Cursor.Position in WinForms
[System.Runtime.InteropServices.DllImport("user32.dll")]
static extern bool SetCursorPos(int x, int y);
[System.Runtime.InteropServices.DllImport("user32.dll")]
public static extern void mouse_event(int dwFlags, int dx, int dy, int cButtons, int dwExtraInfo);
public const int MOUSEEVENTF_LEFTDOWN = 0x02;
public const int MOUSEEVENTF_LEFTUP = 0x04;
//This simulates a left mouse click
public static void LeftMouseClick(int xpos, int ypos)
{
SetCursorPos(xpos, ypos);
mouse_event(MOUSEEVENTF_LEFTDOWN, xpos, ypos, 0, 0);
mouse_event(MOUSEEVENTF_LEFTUP, xpos, ypos, 0, 0);
}
To keep the mouse pressed for a specific duration you can Sleep()
the thread that is executing this function, for example :
mouse_event(MOUSEEVENTF_LEFTDOWN, xpos, ypos, 0, 0);
System.Threading.Thread.Sleep(1000);
mouse_event(MOUSEEVENTF_LEFTUP, xpos, ypos, 0, 0);
The above code will keep the mouse pressed for 1 second unless the user presses the releases the mouse button. Also, make sure to not execute this code on the main UI thread as it will cause it to hang.
Simulate Mouse Click when key is pressed
First make sure the Form KeyPreview
property is set to True
To do the click simulation you need to call MOUSEEVENTF_RIGHTDOWN
and MOUSEEVENTF_RIGHTUP
like so. (also notice i use uint along the way)
[DllImport("user32.dll", CharSet = CharSet.Auto, CallingConvention = CallingConvention.StdCall)]
public static extern void mouse_event(uint dwFlags, uint dx, uint dy, uint cButtons, uint dwExtraInfo);
private const int MOUSEEVENTF_LEFTDOWN = 0x02;
private const int MOUSEEVENTF_LEFTUP = 0x04;
private const int MOUSEEVENTF_RIGHTDOWN = 0x08;
private const int MOUSEEVENTF_RIGHTUP = 0x10;
private void Form1_KeyDown(object sender, KeyEventArgs e)
{
switch (e.KeyCode)
{
case Keys.Insert:
int X = Cursor.Position.X;
int Y = Cursor.Position.Y;
mouse_event(MOUSEEVENTF_RIGHTDOWN | MOUSEEVENTF_RIGHTUP, (uint)X, (uint)Y, 0, 0);
break;
}
}
How to Simulate a mouse/Button Click in c#
I think you made a spelling mistake. It should be button3
instead of Button3
. The same code worked for me:
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
MessageBox.Show("Button1.Clicked");
}
private void button2_Click(object sender, EventArgs e)
{
MessageBox.Show("Button2.Clicked");
}
private void button3_Click(object sender, EventArgs e)
{
button1.PerformClick();
}
}
How to simulate mouseclick from the code?
There isn't a separate event for right/left/other mouse click. There is only a MouseClick
event. But a MouseClick event is a delegate of type MouseEventHandler, a MouseEventHandler receives an argument e
of type MouseEventArgs, a MouseEventArgs instance has a property named Button
of type MouseButtons enumeration, here you can find what kind of button has been pressed. In the link above a lot of examples how to use these infos.
This should answer your question, but the remainder of your problem (passing this information to a remote server) is totally unclear
Related Topics
How to Suspend Painting For a Control and Its Children
Could Not Load File or Assembly or One of Its Dependencies
How to Change Property Names When Serializing With Json.Net
Best Way to Get Application Folder Path
Creating a Blocking Queue≪T≫ in .Net
Mvc5 Razor Html.Dropdownlistfor Set Selected When Value Is in Array
What's the Difference Between Invoke() and Begininvoke()
Why Covariance and Contravariance Do Not Support Value Type
How to Get a User'S Client Ip Address in Asp.Net
How to Get the Webbrowser Control to Show Modern Contents
In C#, Why Is String a Reference Type That Behaves Like a Value Type
How to Encode and Decode a Base64 String
How to Calculate Someone'S Age Based on a Datetime Type Birthday