This question already has answers here:
How do you simulate Mouse Click in C#?
(7 answers)
Closed 7 years ago.
Does anyone knows how to perform a mouse click on a specific point of the desktop? I need a application to perform mouse click on another's application button localized in a specific place on desktop.
This works in WinForms
[DllImport("user32.dll",CharSet=CharSet.Auto, CallingConvention=CallingConvention.StdCall)]
public static extern void mouse_event(long dwFlags, long dx, long dy, long cButtons, long 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;
public void DoMouseClick() {
//Call the imported function with the cursor's current position
int X = Cursor.Position.X;
int Y = Cursor.Position.Y;
mouse_event(MOUSEEVENTF_LEFTDOWN | MOUSEEVENTF_LEFTUP, X, Y, 0, 0);
}
From http://www.gamedev.net/topic/321029-how-to-simulate-a-mouse-click-in-c/
You may look into Coded UI. It's main purpose is for performing UI tests on applications and supports programmatic access to a variety of different application frameworks (WPF, WinForms, VB, etc).
Related
Is there a way to simulate a click at specific coordinates on Windows IoT?
I tried with mouse_event:
mouse_event(MOUSEEVENTF_LEFTDOWN, x, y, 0, 0);
but, I get this error:
System.EntryPointNotFoundException: 'Unable to find an entry point named 'mouse_event' in DLL 'user32.dll'.'.
Is it because that function doesn't exist in the IoT version of Windows?
I saw there was SendInput, but the only syntax on the documentation is in C++. Is it possible at all to use it in C# on Windows IoT and if so how? If you have an example in mind, linking it would be very helpful. I searched around but I couldn't find something that could work on UWP.
Here is the code I used for the mouse_event:
[System.Runtime.InteropServices.DllImport("user32.dll", EntryPoint = "SetCursorPos")]
private 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;
public const int MOUSEEVENTF_RIGHTDOWN = 0x08;
public const int MOUSEEVENTF_RIGHTUP = 0x10;
//...
public void Click(int x, int y)
{
SetCursorPos(x,y);
mouse_event(MOUSEEVENTF_LEFTDOWN, x, y, 0, 0);
mouse_event(MOUSEEVENTF_LEFTUP, x, y, 0, 0);
}
As of Fall Creators Update Release of Windows IOT (SDK version 16299), you can use the InputInjector API in UWP apps: https://learn.microsoft.com/en-us/uwp/api/Windows.UI.Input.Preview.Injection.InputInjector
The API allows injecting mouse input among other input types.
Neither mouse_event nor SendInput can be used in a UWP application. As documented, these API's are for
desktop apps only
If you are running this from a UWP application (which apparently you are), there is no way to automate another UWP application. The sandboxing will not allow this. This includes UI Automation.
I have an old color picker utility written c++ that I coded years back and want to rewrite using c#.
I implemented the global hook to pick pixels off the screen and so on. Everything is ok but...
The cross cursor reverts to the pointer once the mouse moves outside the form and onto the desktop. This does not happen with my c++ code (MFC actually).
How is this accomplished in c#?
Thank you all.
(I'm using this http://www.codeproject.com/Articles/7294/Processing-Global-Mouse-and-Keyboard-Hooks-in-C for the hook)
The solution (or a workaround) is to simulate the first part of a mouse click event. This will lock the mouse on the calling window, thus preserving the chosen cursor.
[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;
And then after enabling the mouse capture in the code:
mouse_event(
MOUSEEVENTF_LEFTDOWN,
(uint)Cursor.Position.X,
(uint)Cursor.Position.Y,
0,
0 );
this.Cursor = Cursors.Cross;
Hope it helps.
I'm busy making a application where i can use the Leap as a mouse. I'm making a WPF application with C# and XAML.
I allready can move the cursor, but i have problems making a function to activate the left mouse button.
Can someone help me with this problem? I need to activate buttons created in XAML.
Another solution could be a function that activates a button when de Leap cursor is on the button for like 3 seconds. I can't find any examples on the Internet. Does someone have a simple basic program or example for me? Please help!
Here is a link to the application i allready have. Maybe it helps
https://www.dropbox.com/sh/kp51hdbhcacaky7/pdinDQpA-6
About the "mouse over button" thing - u can try this solution (worked for me):
create hover event for buttons (mouse entered / leave)
create a bool flag for like "isHovered" (true if hovered, false if not)
start a DispacherTimer, set it's Tick (3 sec?)
on Tick do mouse click (previous answer)
hope it halped :)
try this:
[DllImport("user32.dll", CharSet = CharSet.Auto, CallingConvention = CallingConvention.StdCall)]
public static extern void mouse_event(int dwFlags, int dx, int dy, int cButtons, int 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;
public static void DoMouseClick()
{
Point mousePoint = GetMousePosition();
mouse_event(MOUSEEVENTF_LEFTDOWN | MOUSEEVENTF_LEFTUP, (int)mousePoint.X, (int)mousePoint.Y, 0, 0);
}
Is there any way I can send a left click event to a TextBox? for what ever the reason although I am doing a TextBox.Focus() and the cursor is blinking inside the TextBox but I cannot start typing in it, But if I do an extra click with the mouse inside the text box then I can start typing. So, I was wondering how to send that event to it?
To send mouse events, you'll need to import user32.dll and use mouse_event(long dwFlags, long dx, long dy, long cButtons, long dwExtraInfo);
Example
class Mouse
{
[DllImport("user32.dll", CharSet = CharSet.Auto, CallingConvention = CallingConvention.StdCall)]
public static extern void mouse_event(long dwFlags, long dx, long dy, long cButtons, long dwExtraInfo);
private const int MOUSEEVENTF_LEFTDOWN = 0x02; //Left click
private const int MOUSEEVENTF_LEFTUP = 0x04;
private const int MOUSEEVENTF_RIGHTDOWN = 0x08; //Right click
private const int MOUSEEVENTF_RIGHTUP = 0x10;
public static void sendMouseRightclick(Point p)
{
mouse_event(MOUSEEVENTF_RIGHTDOWN | MOUSEEVENTF_RIGHTUP, p.X, p.Y, 0, 0); //Sends a mouse right click at the specified Point
}
public static void sendMouseClick(Point p)
{
mouse_event(MOUSEEVENTF_LEFTDOWN | MOUSEEVENTF_LEFTUP, p.X, p.Y, 0, 0); //Sends a mouse left click at the specified Point
}
}
When using this, you may call Mouse.sendMouseClick(Point p) to send a mouse left click at the specified point.
In your case, I think that you might want to use Mouse.sendMouseClick(TextBox.Location); to send a mouse left click at the current TextBox position.
Thanks,
Have a great day :)
Ok to fix this first we should make sure the form itsself is activate.
To do that, in the Form_Shown event we should call this.Activate()
Here is the original question but it is considered to java:
Simulate mouse clicks at a certain position on inactive window in Java?
Anyways, I'm building a bot to run in the background. This bot requires me to click. Of course, I want to be able to do other things while the bot is running.
So I was wondering if it was possible for me to simulate a mouse click at a certain position on an inactive window.
If this is possible, I would greatly appreciate it if any of you could help me.
Thanks!
Yes it is possible, here's the code I used for a previous school project:
[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;
public const int MOUSEEVENTF_RIGHTDOWN = 0x08;
public const int MOUSEEVENTF_RIGHTUP = 0x10;
//This simulates a left mouse click
public static void LeftMouseClick(Point position)
{
Cursor.Position = position;
mouse_event(MOUSEEVENTF_LEFTDOWN, position.X, position.Y, 0, 0);
mouse_event(MOUSEEVENTF_LEFTUP, position.X, position.Y, 0, 0);
}
EDIT : It seems that the mouse_event function was replaced by SendInput() but it still works (Windows 7 and earlier)