Problem with virtual mouse click - c#

I have a program that simulates mouse click.
Code is something like this:
[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;
public static void DoMouseClick(int x, int y)
{
Cursor.Position = new Point(x, y);
mouse_event(MOUSEEVENTF_LEFTDOWN | MOUSEEVENTF_LEFTUP, x, y, 0, 0);
}
This code works perfectly. For example I call this function every 30 minutes. But if I press WINKEY+L (Windows is locked) only cursor is moved but not press occurs.
Any ideas?

The Login screen on windows is designed to NOT allow clicks and automation of UI, as a security feature, IIRC

Related

How to perform mouse click action when looping

I need to make some automatic clicks in certain positions, but when I put the method inside the FOR the click action is only executed in the last loop.
[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);
//Mouse actions
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
mouse_event(MOUSEEVENTF_LEFTDOWN | MOUSEEVENTF_LEFTUP, 0, 0, 0, 0);
}
[DllImport("user32.dll")]
static extern bool SetCursorPos(int X, int Y);
public static void MoveCursorToPoint(int x, int y)
{
SetCursorPos(x, y);
}
How I wish to use:
for (int i = 0; i <= 3; i++)
{
MoveCursorToPoint(100, 100);
DoMouseClick();
}
The click action works perfectly, however when I put in some loop the program only clicks when it is in the last loop.
How to do the click action whenever it is requested inside the looping?
It may be happening too fast. Try adding a small delay after the click event to allow the OS to have time to process it before firing more.

simulate mouse left click on a target and then move mouse to the previous position in C# .net

I needed to simulate mouse left click in some scenario in the code (c# .net).
this simulation must be done but it must be invisible for users. so when left click is going to occur I save cursor position. After doing left click I should set cursor position to the save value.
there is a problem.
int save_cursur_x = Cursor.Position.X;
int save_cursur_y = Cursor.Position.Y;
int current_node_x = newGraphEditor.getCurrentNodeGlobalPosition_x();
int current_node_y = newGraphEditor.getCurrentNodeGlobalPosition_y();
LeftMouseClick(current_node_x, current_node_y);
Cursor.Position = new Point(save_cursur_x, save_cursur_y);
When the last line ( Cursor.Position = new Point(save_cursur_x, save_cursur_y)) is removed left click happen correctly on the target position. But when I add that line, left click happen on a unknown position (or maybe left click doesn't happen)
This is LeftMouseClick method :
//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);
}
Update:
There is a selection delegate where target position set. this delegate should run exactly when you click on target. I think problem is here . After left click on target this delegate isn't run . After setting Cursor position to previous value delegate is run. so target position doesn't set correctly because now cursor isn't on target.(Its position has changed)
ItemEventHandler<IModelItem> selectionDelegate =
delegate (object source, ItemEventArgs<IModelItem> args)
{
var tag = ((INode)(args.Item)).Tag;
if (tag is HostInnerNodeTag)
{
currentNodeGlobalPosition_x = Cursor.Position.X;
currentNodeGlobalPosition_y = Cursor.Position.Y;
}
}
You can use SendMessage instead.
[DllImport("user32.dll")]
public static extern int SendMessage(
IntPtr hWnd, // handle to destination window
uint Msg, // message
IntPtr wParam, // first message parameter
IntPtr lParam // second message parameter
);
public const uint WM_LBUTTONDOWN = 0x0201;
public const uint WM_LBUTTONUP = 0x0202;
//This simulates a left mouse click
public static void LeftMouseClick(IntPtr hwnd, uint xpos, uint ypos)
{
SendMessage(hwnd, WM_LBUTTONDOWN, new IntPtr(xpos), new IntPtr(ypos));
SendMessage(hwnd, WM_LBUTTONUP, new IntPtr(xpos), new IntPtr(ypos));
}
private void send()
{
LeftMouseClick(newGraphEditor.Handle, 10, 10);
}

C#: Drag any window from code behind (programmatically)

Please let me elaborate what I am doing.
I am creating a screen share application. Where there are 2 apps first is a windows application (whose screen is getting shared) and the other is a browser web app (On which the user is watching the screen). The browser web app is sending client x and y coordinates of the mouse to the windows app. The windows app is using the x and y coordinates to move the mouse using the given below code.
[DllImport("user32.dll")]
static extern bool SetCursorPos(int X, int Y);
public static void MoveCursorToPoint(int x, int y)
{
SetCursorPos(x, y);
}
The browser app is also sending mouse clicks and the windows app is performing the clicks using the given below code.
[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 static void DoMouseClick()
{
mouse_event(MOUSEEVENTF_LEFTDOWN | MOUSEEVENTF_LEFTUP, 0, 0, 0, 0);
}
Problem/Challenge
The problem or a challenge here is to perform the drag operation. For an example how will I drag the window of any application from one place to the another place from C# code.
Many thanks for your attention.
Finally, here is the code that will do all the system mouse events. In order to perform the drag and drop operation, you will need to First Send MouseDown(Mouse Click) and keep it Clicked While Changing the Mouse Position than Send MouseUp(Release Click) something like this.
MouseHelper mh = new MouseHelper();
mh.sendMouseDown();
Cursor.Position = new Point(30, 30);
mh.sendMouseUp();
Here is the whole helper file
public class MouseHelper
{
[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);
public static extern void mouse_event(uint dwFlags, uint dx, uint dy, uint cButtons, uint dwExtraInfo);
private const uint MOUSEEVENTF_LEFTDOWN = 0x02;
private const uint MOUSEEVENTF_LEFTUP = 0x04;
private const uint MOUSEEVENTF_RIGHTDOWN = 0x08;
private const uint MOUSEEVENTF_RIGHTUP = 0x10;
public void sendMouseRightclick(Point p)
{
mouse_event((uint)MOUSEEVENTF_RIGHTDOWN | (uint)MOUSEEVENTF_RIGHTUP, (uint)p.X, (uint)p.Y, (uint)0, (uint)0);
}
public void sendMouseDoubleClick(Point p)
{
mouse_event((uint)MOUSEEVENTF_LEFTDOWN | (uint)MOUSEEVENTF_LEFTUP, (uint)p.X, (uint)p.Y, (uint)0, (uint)0);
Thread.Sleep(150);
mouse_event((uint)MOUSEEVENTF_LEFTDOWN | (uint)MOUSEEVENTF_LEFTUP, (uint)p.X, (uint)p.Y, 0, 0);
}
public void sendMouseRightDoubleClick(Point p)
{
mouse_event((uint)MOUSEEVENTF_RIGHTDOWN | (uint)MOUSEEVENTF_RIGHTUP, (uint)p.X, (uint)p.Y, 0, 0);
Thread.Sleep(150);
mouse_event((uint)MOUSEEVENTF_RIGHTDOWN | (uint)MOUSEEVENTF_RIGHTUP, (uint)p.X, (uint)p.Y, 0, 0);
}
public void sendMouseDown()
{
mouse_event(MOUSEEVENTF_LEFTDOWN, 50, 50, 0, 0);
}
public void sendMouseUp()
{
mouse_event(MOUSEEVENTF_LEFTUP, 50, 50, 0, 0);
}
}
Source The given code at this source link will give an error
Additional Information: A call to PInvoke function 'KinectHandTrackmyApping!myApp.MainWindow::mouse_event' has unbalanced the stack. This is likely because the managed PInvoke signature does not match the unmanaged target signature. Check that the calling convention and parameters of the PInvoke signature match the target unmanaged signature.
In order resolve the error I have changed
public static extern void mouse_event(long dwFlags, long dx, long dy, long cButtons, long dwExtraInfo);
to this
public static extern void mouse_event(uint dwFlags, uint dx, uint dy, uint cButtons, uint dwExtraInfo);
The error was usage of long because long gives an exception because it's a 64-bit, uint works because it's 32-bit -- but it won't work for negative coordinates (which is a common monitor setup in Windows).

C# mouse_event click doesn't work in my case

I'm using a c# code to click on other process window,It's work great but it doesn't work when i want to click in Bluestack(Android Emulator) window.
any idea ?
the code :
[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);
}
Maybe you need to make a virtual mouse device to work correctly
It will be difficult to make maybe try to use external DLLs

Simulating mouse move in Math Input Panel (Windows 7)

I would like to compare my application for handwritten mathematical symbols recognition with the Math Input Panel (MIP) contained in Windows 7. I have a library of recorded mouse strokes representing different mathematical formulas and I need to send them to the MIP to measure its performance.
I tried to simulate mouse move but it's not working.
Here are constants and imported methods that I use:
const UInt32 MOUSEEVENTF_LEFTDOWN = 0x0002;
const UInt32 MOUSEEVENTF_LEFTUP = 0x0004;
[DllImport("user32.dll", CharSet = CharSet.Unicode)]
public static extern IntPtr FindWindow(string lpClassName, string lpWindowName);
[DllImport("user32.dll")]
[return: MarshalAs(UnmanagedType.Bool)]
public static extern bool SetForegroundWindow(IntPtr hWnd);
[DllImport("user32.dll")]
static extern void mouse_event(UInt32 dwFlags, UInt32 dx, UInt32 dy, UInt32 dwData, UIntPtr dwExtraInfo);
And that's the code itself:
IntPtr windowHandle = FindWindow("MathInput_Window", "");
SetForegroundWindow(windowHandle);
Cursor.Position = new Point(600, 700);
mouse_event(MOUSEEVENTF_LEFTDOWN, 600, 700, 0, UIntPtr.Zero);
for (int x = 600; x <= 650; x++)
{
Cursor.Position = new Point(x, 700);
}
for (int y = 700; y <= 750; y++)
{
Cursor.Position = new Point(650, y);
}
mouse_event(MOUSEEVENTF_LEFTUP, 650, 750, 0, UIntPtr.Zero);
But the only thing i get is a single dot at the position [600,700]. The funny thing is that when I use MSPaint instead of MIP everything is working perfectly.
Does anyone have some idea how it could be solved?
I have already solved the problem using the following function:
[DllImport("user32.dll", SetLastError = true)]
static extern uint SendInput(uint nInputs, ref INPUT pInputs, int cbSize);
I call it to control both mouse button clicks and mouse moves and it works perfectly.

Categories