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
Related
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.
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).
I have a small application I wrote that simply displays a preview of webcam or my capture card. At the moment it works exactly how I want it to, with the exception that the capture card displays at a much lower framerate than I'd like it to.
Here is my relevant code:
private const int WM_CAP_DRIVER_CONNECT = 1034;
private const int WM_CAP_SET_PREVIEW = 1074;
private const int WM_CAP_SET_PREVIEWRATE = 1076;
private const int WM_CAP_SET_SCALE = 1077;
private const int WS_CHILD = 1073741824;
private const int WS_VISIBLE = 268435456;
private const short SWP_NOMOVE = 2;
private const short SWP_NOZORDER = 4;
private const short HWND_BOTTOM = 1;
private const int iDevice = 0;
private int hHwnd;
private int previewRate = 34;
private int width = 640;
private int height = 480;
[DllImport("user32.dll", EntryPoint="SendMessageA")]
public static extern int SendMessage(int hWnd, uint Msg, int wParam, int lParam);
[DllImport("user32.dll", EntryPoint="SetWindowPos")]
static extern int SetWindowPos(int hwnd, int hWndInsertAfter, int x, int y, int cx, int cy, int wFlags);
[DllImport("user32.dll")]
static extern bool DestroyWindow(int hndw);
[DllImport("avicap32.dll")]
public static extern int capCreateCaptureWindow(string lpszWindowName, int dwStyle, int X, int Y, int nWidth, int nHeight, int hwndParent, int nID);
private void OpenPreviewWindow()
{
hHwnd = capCreateCaptureWindow(iDevice.ToString(), (WS_VISIBLE | WS_CHILD), 0, 0, width, height, Handle.ToInt32(), 0);
// Connect to device
if (SendMessage(hHwnd, WM_CAP_DRIVER_CONNECT, iDevice, 0) != -1)
{
SendMessage(hHwnd, WM_CAP_SET_SCALE, 1, 0);
SendMessage(hHwnd, WM_CAP_SET_PREVIEWRATE, previewRate, 0);
SendMessage(hHwnd, WM_CAP_SET_PREVIEW, 1, 0);
SetWindowPos(hHwnd, HWND_BOTTOM, 0, 0, width, height, (SWP_NOMOVE | SWP_NOZORDER));
}
else
{
DestroyWindow(hHwnd);
}
}
When I preview the capture card in an application like FMLE, it previews at 30 FPS, which is my target framerate (~34 milliseconds per frame,) however when I use my application to preview it's closer to 10-15 FPS. I should probably also note that my program will preview 30 FPS from my webcam. What could be causing the problem with the capture card, and how can I fix it?
Use DirectShow to view the webcam.
An example would be: http://www.codeproject.com/KB/audio-video/WebcamUsingDirectShowNET.aspx
or http://www.codeproject.com/KB/miscctrl/webcam_c_sharp.aspx
So I'm trying to simulate the left mouse click and the left mouse release to do some automated dragging and dropping.
It's currently in a C# Winforms (Yes, winforms :|) and is being a bit of a goose.
Basically, once a Click is sent, I want it to update the cursor position based upon the Kinect input. The Kinect side of things is fine but i'm not sure how to find if the button is still pressed or not.
here's the code i'm currently using + some psuedocode to help better explain myself (the do while).
class MouseImpersonator
{
[DllImport("user32.dll", CharSet = CharSet.Auto, CallingConvention = CallingConvention.StdCall)]
private static extern void mouse_event(uint dwFlags, uint dx, uint dy, uint cButtons, uint dwExtraInfo);
private const int leftDown = 0x02;
private const int leftUp = 0x04;
public static void Grab(int xPos, int yPos)
{
Cursor.Position = new Point(xPos + 25, yPos + 25);
mouse_event(leftDown, (uint) xPos, (uint) yPos, 0, 0);
//do
//{
//Cursor.Position = new Point(KinectSettings.movement.LeftHandX, KinectSettings.movement.LeftHandY);
//} while (the left mouse button is still clicked);
}
public static void Release(int xPos, int yPos)
{
Cursor.Position = new Point(xPos + 25, yPos + 25);
mouse_event(leftUp, (uint) xPos, (uint) yPos, 0, 0);
}
}
I've had a hunt of the google and can't find anything for what I need except for a WPF equivalent: http://msdn.microsoft.com/en-us/library/system.windows.input.mouse.aspx
I'm a bit out of my depth, but any help is greatly appreciated.
Lucas.
-
The Easiest answer was infact to use a bool and just check to see what's going on.
I started it on a new thread so it didn't break everything else.
Idealy you'd tidy this up a little bit.
public static void Grab(int xPos, int yPos)
{
_dragging = true;
Cursor.Position = new Point(xPos, yPos + offSet);
mouse_event(leftDown, (uint) xPos, (uint) yPos, 0, 0);
var t = new Thread(CheckMouseStatus);
t.Start();
}
public static void Release(int xPos, int yPos)
{
_dragging = false;
Cursor.Position = new Point(xPos, yPos + offSet);
mouse_event(leftUp, (uint) xPos, (uint) yPos, 0, 0);
}
private static void CheckMouseStatus()
{
do
{
Cursor.Position = new Point(KinectSettings.movement.HandX, KinectSettings.movement.HandY + offSet);
}
while (_dragging);
}
The following code should return true if the left mouse button is down, false if it is up, Control being System.Windows.Forms.Control:
Control.MouseButtons.HasFlag(MouseButtons.Left)
p.s. documentation for this can be found on MSDN here.
[DllImport("user32.dll", CharSet = CharSet.Auto, CallingConvention = CallingConvention.StdCall)]
private static extern void mouse_event(uint dwFlags, int dx, int dy, uint cButtons, uint dwExtraInfo);
[DllImport("user32.dll")]
static extern bool SetCursorPos(int X, int Y);
const uint MOUSEEVENTF_LEFTDOWN = 0x0002;
const uint MOUSEEVENTF_LEFTUP = 0x0004;
const uint MOUSEEVENTF_MOVE = 0x0001;
static void Drag(int startX,int startY,int endX,int endY)
{
endX = endX - startX;
endY = endY - startY;
SetCursorPos(startX, startY);
mouse_event(MOUSEEVENTF_LEFTDOWN, 0, 0, 0, 0);
mouse_event(MOUSEEVENTF_MOVE, endX, endY, 0, 0);
mouse_event(MOUSEEVENTF_LEFTUP, 0, 0, 0, 0);
}
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