how to use PostMessage / SendMessage?

hi, i need to send keystrokes an application (including, "enter", "left arrow", "right arrow", etc).

but how do i do that? i do not see any short and useful examples to doing it.

all i have got at the current moment is to get the foreground window using

HWND foregroundWindow = GetForegroundWindow();

i see the PostMessage function takes in HWND, UINT, WPARAM and LPARAM... but i have no idea which is for what. can somebody pls explain and teach me how to send keystrokes to an arbitrary application?

thanks.
Last edited on
Keystrokes are reported to an application's Window Procedure through WM_CHAR and WM_KEYDOWN messages (there are also a few others I won't mention). Therefore, speaking in general terms, if you want to either SendMessage() or PostMessage() a message to a Window Procedure, you need to look up the message in your Windows Api documentation, i.e., in the case of WM_CHAR look up WM_CHAR, and that way you will find what needs to be put by you in the various 16 bit halves of the wParam and lParam parameters. Usually various macros are of help here such as MAKELONG, MAKEWPARAM, MAKELPARAM, etc. After you've filled these out you just Send or Post the message.

I do this occasionally myself. However, I don't recall ever trying it to send keystrokes. Without trying it I can't say if it works or not. You'll just have to try and see. That's why I used the term 'speaking in general terms' above.
Topic archived. No new replies allowed.