Can't send key to window with SendMessage

Posted by user297313 on Stack Overflow See other posts from Stack Overflow or by user297313
Published on 2010-03-19T11:49:59Z Indexed on 2010/03/19 11:51 UTC
Read the original article Hit count: 272

Filed under:

I'm writing a C program under Windows that should send an ENTER key to a dialog box to close it automatically.

I retrieve the handle to the top level window I'm interested in (by means of EnumDesktopWindows()) and then try to send an 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 );

and so on...

As a possibly simpler scenario, I also tried to send an ascii key to, say, notepad.

How is this supposed to work?

Thanks in advance

© Stack Overflow or respective owner

Related posts about Windows