Quantcast
Channel: CodeGuru Forums - C++ and WinAPI
Viewing all articles
Browse latest Browse all 583

how use SendInput() for a non focus window?

$
0
0
i did a nice functions for use the SendInput():
Code:

//Simulate the press key:
void inputDown(INPUT &ip, WORD wrdKey, int MouseX=-1, int MouseY=-1)
{
    if(wrdKey == VK_LBUTTON || wrdKey == VK_RBUTTON || wrdKey == VK_MBUTTON || wrdKey == VK_XBUTTON1 || wrdKey == VK_XBUTTON2)
    {
        INPUT input;
        input.type = INPUT_MOUSE;
        input.mi.mouseData=0;
        POINT mousepos;
        GetCursorPos(&mousepos);
        if(MouseX==-1)
            input.mi.dx =  mousepos.x;//x being coord in pixels
        else
          input.mi.dx =MouseX;
        if(MouseX==-1)
            input.mi.dy = mousepos.y;//y being coord in pixels
        else
            input.mi.dy =MouseY;
        if(wrdKey==VK_LBUTTON)
            input.mi.dwFlags = MOUSEEVENTF_LEFTDOWN;
        else if(wrdKey==VK_RBUTTON)
            input.mi.dwFlags = MOUSEEVENTF_RIGHTDOWN;
        else if(wrdKey==VK_MBUTTON)
            input.mi.dwFlags = MOUSEEVENTF_MIDDLEDOWN;
        else if(wrdKey==VK_XBUTTON1 || wrdKey==VK_XBUTTON2)
            input.mi.dwFlags = MOUSEEVENTF_XDOWN;
        SendInput(1,&input,sizeof(input));
    }
    else
    {
        ip.type = INPUT_KEYBOARD;
        ip.ki.wScan = 0; // hardware scan code for key
        ip.ki.time = 0;
        ip.ki.dwExtraInfo = 0;

        ip.ki.wVk = wrdKey;
        ip.ki.dwFlags = 0; // 0 for key press
        SendInput(1, &ip, sizeof(INPUT));
    }

}


//Simulate the Release key:
void inputRelease(INPUT &ip, WORD wrdKey, int MouseX=-1, int MouseY=-1)
{
    if(wrdKey == VK_LBUTTON || wrdKey == VK_RBUTTON || wrdKey == VK_MBUTTON || wrdKey == VK_XBUTTON1 || wrdKey == VK_XBUTTON2)
    {
        INPUT input;
        input.type = INPUT_MOUSE;
        input.mi.mouseData=0;
        POINT mousepos;
        GetCursorPos(&mousepos);
        if(MouseX==-1)
            input.mi.dx =  mousepos.x;//x being coord in pixels
        else
          input.mi.dx =MouseX;
        if(MouseX==-1)
            input.mi.dy =  mousepos.y;//y being coord in pixels
        else
            input.mi.dy =MouseY;
        if(wrdKey==VK_LBUTTON)
            input.mi.dwFlags = MOUSEEVENTF_LEFTUP;
        else if(wrdKey==VK_RBUTTON)
            input.mi.dwFlags = MOUSEEVENTF_RIGHTUP;
        else if(wrdKey==VK_MBUTTON)
            input.mi.dwFlags = MOUSEEVENTF_MIDDLEUP;
        else if(wrdKey==VK_XBUTTON1 || wrdKey==VK_XBUTTON2)
            input.mi.dwFlags = MOUSEEVENTF_XUP;
        SendInput(1,&input,sizeof(input));
    }
    else
    {
        ip.type = INPUT_KEYBOARD;
        ip.ki.wScan = 0; // hardware scan code for key
        ip.ki.time = 0;
        ip.ki.dwExtraInfo = 0;

        ip.ki.wVk =wrdKey;
        ip.ki.dwFlags = KEYEVENTF_KEYUP; // KEYEVENTF_KEYUP for key release
        SendInput(1, &ip, sizeof(INPUT));
    }

}

//combine the inputDown with inputUp functions:
void inputPress(WORD wrdKey, WORD blnShift=-1,  int MouseX=-1, int MouseY=-1)
{
    INPUT ip;
    //pressing keys
    //remember: with combination keys, 1st the control\****\menu key and then the letter key!!
    if((blnShift&VK_CONTROL)==VK_CONTROL)
    {
        inputDown(ip,blnShift);
    }
    if((blnShift&VK_MENU)==VK_MENU)
    {
        inputDown(ip,blnShift);
    }
    if((blnShift&VK_SHIFT)==VK_SHIFT)
    {
        inputDown(ip,blnShift);
    }

    inputDown(ip,wrdKey,MouseX,MouseY);

    //release key:
    inputRelease(ip,wrdKey,MouseX,MouseY);

    if((blnShift&VK_CONTROL)==VK_CONTROL)
    {
        inputRelease(ip,blnShift);
    }
    if((blnShift&VK_MENU)==VK_MENU)
    {
        inputRelease(ip,blnShift);
    }
    if((blnShift& VK_SHIFT)==VK_SHIFT)
    {
        inputRelease(ip,blnShift);
    }
}

but these code only works for a focus window.
how can i change these code for works with a non focus window(like choose a window)?

Viewing all articles
Browse latest Browse all 583

Trending Articles