SendInput() not equal to pressing key manually on keyboard in C++?

前端 未结 3 1645
时光取名叫无心
时光取名叫无心 2020-11-28 08:55

I wanted to write a c++ code to emulate pressing a keyboard key \"A\":

// Set up a generic keyboard event.
ip.type = INPUT_KEYBOARD;
ip.ki.wScan = 0; // hard         


        
3条回答
  •  感情败类
    2020-11-28 09:26

    You often need to set the scan code:

    // Set up a generic keyboard event.
    ip.type = INPUT_KEYBOARD;
    ip.ki.wScan = MapVirtualKey(code, MAPVK_VK_TO_VSC); // hardware scan code for key
    ip.ki.time = 0;
    ip.ki.dwExtraInfo = 0;
    
    // Press the "..." key
    ip.ki.wVk = code; // virtual-key code for the "a" key
    ip.ki.dwFlags = 0; // 0 for key press
    SendInput(1, &ip, sizeof(INPUT));
    

    And building an array as IInspectable suggests is also definitely the way to go.

提交回复
热议问题