0
HWND hWindowHandle = FindWindow(NULL, L"Untitled - Блокнот");
HWND EditClass = FindWindowEx(hWindowHandle, NULL, L"Edit", NULL);
PostMessage(EditClass, WM_KEYDOWN, 0x5A, 0x002C0001);
PostMessage(EditClass, WM_CHAR, 0x7A, 0x002C0001);
PostMessage(EditClass, WM_KEYUP, 0x5A, 0xC02C0001);

I used the code above on Windows 10 64-bit and everything worked. But on Windows 7 64-bit Ultimate, it doesn't work. It doesn't send a message to Notepad, although if I compare messages in Spy++ when I click and what I send, they are the same. But when I try to send a message, it is not even visible in Spy++.

Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770
  • 5
    [You can't simulate keyboard input with PostMessage](https://devblogs.microsoft.com/oldnewthing/20050530-11/?p=35513). – IInspectable Jan 24 '23 at 14:22
  • Did you verify that `FindWindow/Ex()` are not failing and returning `NULL` handles, or that `PostMessage()` is not failing and returning `FALSE`? All 5 API calls you are making could fail, so where's your error handling? – Remy Lebeau Jan 24 '23 at 17:35
  • @RemyLebeau I got this working code from the same forum. Originally it was SendMessage, but through spy I realized that I should use PostMessage. But tried both options. The code worked on the 10 win, why doesn't it work on the 7? Any ideas? – Вили Били Jan 25 '23 at 06:13
  • @IInspectable I send a message to an inactive window – Вили Били Jan 25 '23 at 06:15
  • You can't automate a UI like that. I explained this [here](https://stackoverflow.com/a/74898459/1889329). – IInspectable Jan 25 '23 at 07:56
  • @ВилиБили this is not an OS issue. These APIs work the same way on all versions. Maybe the window can't be found because the title is different than your are expecting (check with `EnumWindows()` to see what windows are actually available). Maybe the window is running elevated and can't be sent to (ie, UIPI blocking the messages). There is no way to know until you check return values and error codes for failures. So, I ask again, did you VERIFY the functions are not failing? If they are, did you check `GetLastError()` to find out WHY they failed? – Remy Lebeau Jan 25 '23 at 15:38

0 Answers0