How to send keydown event to inactive window in C++?

不打扰是莪最后的温柔 提交于 2019-12-09 07:37:26

Ok. Use Spy++ and hook messages received by Notepad when you press Z key. That way you can simulate/emulate EXACTLY same thing, so it will look like exactly as user pressed Z key. Also you need to find Edit class in Notepad to send messages. So I did this, I ran Spy++, hooked messages, and wrote the same thing. Now it works:

#include <windows.h>
#include <iostream>
#include <string>



int main()
{
    LPCSTR Target_window_Name = "Untitled - Notepad"; //<- Has to match window name
    HWND hWindowHandle = FindWindow(NULL,Target_window_Name);
    HWND EditClass = FindWindowEx(hWindowHandle, NULL, "Edit", NULL);

    SendMessage(EditClass,WM_KEYDOWN,0x5A,0x002C0001);
    SendMessage(EditClass,WM_CHAR,0x7A,0x002C0001);
    SendMessage(EditClass,WM_KEYUP,0x5A,0xC02C0001);

   return(0);
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!