2

I use C++ program to interact with my web, it will simulate the keys pressing when print page show . key pressing event is CTRL + SHIFT + P .

Using SendInput send pressing event (ctrl + shift + p) for my web page(in Chorme) , function is ok for web page, I have tested it, but it is invalid when the print page show .

why print page show and the SendInput is invalid ? Is print page forbidden key message for ?

 int main() {
  HWND parentHwnd, childHwnd;
  parentHwnd = FindWindow(NULL,"XiaoYouCha - Google Chrome");
  if (parentHwnd)
  {
    childHwnd = FindWindowEx(parentHwnd, NULL, NULL, NULL);
    SetForegroundWindow(parentHwnd);

    Sleep(1000);
    ShowPrint();
  }
}

void ShowPrint()
{

INPUT inputs[6] = {};
ZeroMemory(inputs, sizeof(inputs));

inputs[0].type = INPUT_KEYBOARD;
inputs[0].ki.wVk = VK_LCONTROL;
inputs[0].ki.wScan = MapVirtualKeyEx(VK_LCONTROL, 0, (HKL)0xf0010413);

inputs[1].type = INPUT_KEYBOARD;
inputs[1].ki.wVk = VK_LSHIFT;
inputs[1].ki.wScan = MapVirtualKeyEx(VK_LSHIFT, 0, (HKL)0xf0010413);

inputs[2].type = INPUT_KEYBOARD;
inputs[2].ki.wVk = 'P';
inputs[2].ki.wScan = MapVirtualKeyEx('P', 0, (HKL)0xf0010413);

inputs[3].type = INPUT_KEYBOARD;
inputs[3].ki.wVk = 'P';
inputs[3].ki.dwFlags = KEYEVENTF_KEYUP;

inputs[4].type = INPUT_KEYBOARD;
inputs[4].ki.wVk = VK_LSHIFT;
inputs[4].ki.dwFlags = KEYEVENTF_KEYUP;

inputs[5].type = INPUT_KEYBOARD;
inputs[5].ki.wVk = VK_LCONTROL;
inputs[5].ki.dwFlags = KEYEVENTF_KEYUP;

UINT uSent = SendInput(ARRAYSIZE(inputs), inputs, sizeof(INPUT));
if (uSent != ARRAYSIZE(inputs))
{
    printf("SendInput failed: 0x%x\n", HRESULT_FROM_WIN32(GetLastError()));
} 
}
will
  • 195
  • 3
  • 12
  • Specifying both `wVk` and `wScan` looks odd. Also, if you use `wScan`, shouldn't you use add `KEYEVENTF_SCANCODE` to `dwFlags`? – Ted Lyngmo Jul 27 '21 at 12:51
  • 2
    `SendInput` is the wrong tool to implement [UI Automation](https://learn.microsoft.com/en-us/windows/win32/winauto/entry-uiauto-win32). – IInspectable Jul 27 '21 at 14:01
  • 1
    When Chrome's print preview window is open, it will not respond to Ctrl+Shift+P, even if you type it in manually. You have to click the "Print" button instead. To automate the process, you have to figure out if Print Preview is active. For that, you will have to use UI Automation to figure what Chrome is doing. Then use UIA to find the Print button and invoke it. – Barmak Shemirani Jul 27 '21 at 20:55
  • @BarmakShemirani Thanks a lot . I got the answer , It have to use UI Automation . it is a new tech for me . I will learn it . – will Jul 28 '21 at 00:40
  • @IInspectable thanks a lot . I got the right answer from you . – will Jul 28 '21 at 00:42
  • 1
    Try this [example](https://stackoverflow.com/questions/48504300/get-active-tab-url-in-chrome-with-c/48507146#48507146) for accessing chrome with UI. I am not sure how to find the print window though. You may have better luck if you search through .net example. – Barmak Shemirani Jul 28 '21 at 01:45
  • 1
    @TedLyngmo you are right , SendInput is not correctly used . thanks . – will Jul 28 '21 at 05:04

0 Answers0