Unable to send key to window using SendMessage

I am writing a program for Windows, which should send the Enter key to the dialog box to close it automatically.

I extract the handle to the top-level window that interests me (using EnumDesktopWindows ()), and then try sending the ENTER key using SendMessage (note also that closing the window by sending WM_CLOSE works fine).

None of the following works:

SendMessage( hTargetWindow, WM_CHAR, VK_RETURN, 0 );

SendMessage( hTargetWindow, WM_CHAR, VK_RETURN, 1 );

SendMessage( hTargetWindow, WM_KEYDOWN, VK_RETURN, 1 );
SendMessage( hTargetWindow, WM_KEYUP, VK_RETURN, 1 );

SendMessage( hTargetWindow, WM_KEYDOWN, VK_RETURN, 1 );
SendMessage( hTargetWindow, WM_CHAR, VK_RETURN, 1 );
SendMessage( hTargetWindow, WM_KEYUP, VK_RETURN, 1 );

etc.

As a possibly simpler scenario, I also tried sending the ascii key, say, to notepad.

How should this work?

Thanks in advance

+3
source share
4 answers

None of the methods suggested by Nick D work! Surprisingly, the following works:

PostMessage(hTargetWindow, WM_KEYDOWN, VK_RETURN, 0);

PostMessage SendMessage. Windows, .

, , : ENTER (BTW, enableDefaultReply Win XP Embedded). , , , : , . .

PostMessage!

+4

SendMessage(hTargetWindow, WM_KEYDOWN, VK_RETURN, 0);

, , , .

ASCII:

PostMessage(hTargetWindow, WM_CHAR, ch, 0);
+3

Depending on the version of Windows, sending messages may not work if you send a message to an elevated application (that is, an application with administrator privileges) from a failed one. Could this be the culprit?

0
source

Source: https://habr.com/ru/post/1737672/


All Articles