Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How i can simulate a double mouse click on window ( i khow handle) on x, y coordinate, using SendInput?

How i can simulate a double mouse click on window ( i know handle of this window) on x, y coordinate, using SendInput?

like image 626
G-71 Avatar asked Apr 26 '11 11:04

G-71


3 Answers

void DoubleClick(int x, int y)
{
    const double XSCALEFACTOR = 65535 / (GetSystemMetrics(SM_CXSCREEN) - 1);
    const double YSCALEFACTOR = 65535 / (GetSystemMetrics(SM_CYSCREEN) - 1);

    POINT cursorPos;
    GetCursorPos(&cursorPos);

    double cx = cursorPos.x * XSCALEFACTOR;
    double cy = cursorPos.y * YSCALEFACTOR;

    double nx = x * XSCALEFACTOR;
    double ny = y * YSCALEFACTOR;

    INPUT Input={0};
    Input.type = INPUT_MOUSE;

    Input.mi.dx = (LONG)nx;
    Input.mi.dy = (LONG)ny;

    Input.mi.dwFlags = MOUSEEVENTF_MOVE | MOUSEEVENTF_ABSOLUTE | MOUSEEVENTF_LEFTDOWN | MOUSEEVENTF_LEFTUP;

    SendInput(1,&Input,sizeof(INPUT));
    SendInput(1,&Input,sizeof(INPUT));

    Input.mi.dx = (LONG)cx;
    Input.mi.dy = (LONG)cy;

    Input.mi.dwFlags = MOUSEEVENTF_MOVE | MOUSEEVENTF_ABSOLUTE;

    SendInput(1,&Input,sizeof(INPUT));
}

You can use GetWindowRect() to get the window position from its handle and pass relative x and y to DoubleClick function:

RECT rect;
GetWindowRect(hwnd, &rect);

HWND phwnd = GetForegroundWindow();

SetForegroundWindow(hwnd);

DoubleClick(rect.left + x, rect.top + y);

SetForegroundWindow(phwnd); // To activate previous window
like image 58
fardjad Avatar answered Nov 08 '22 20:11

fardjad


This will simulate a double click at certain coordinates

  SetCursorPos(X,Y);
  mouse_event(MOUSEEVENTF_LEFTDOWN | MOUSEEVENTF_LEFTUP,0,0,0,0);
  mouse_event(MOUSEEVENTF_LEFTDOWN | MOUSEEVENTF_LEFTUP,0,0,0,0);
like image 4
McCormick32 Avatar answered Nov 08 '22 19:11

McCormick32


You can specify your goal (best) or the method, but if you try to specify both, more often than not the answer is going to be "that doesn't work like that".

SendInput doesn't work like that, it simulates mouse activity on the screen, which will be delivered to whatever window is visible at that location (or has mouse capture), not the window of your choice.

To deliver a double-click to a specific window, try PostMessage(hwnd, WM_LBUTTONDBLCLK, 0, MAKELPARAM(x, y)).

like image 2
Ben Voigt Avatar answered Nov 08 '22 20:11

Ben Voigt