PostMessage function

Hello! I am trying to automate an installation process and it is going pretty good. I've got it up and running but thought i could improve it by moving away from keybd_event() and into PostMessage() to simulate keyboard events instead, as this would open up the ability for multithreaded installation.

However, i do not get PostMessage() to work towards a console application called "Tera Term" (basically a console input window). I did get PostMessage and even SendMessage to work towards notepad, where i would use this code


PostMessage(hwnd, WM_KEYDOWN, VK_DOWN, 0);

to simulate an arrow down key action, which works totally fine in notepad! I tried the above code towards the main handle (HWND hwnd) of my window but that doesn't work, then i tried to use EnumChildWindows() in order to retrieve potential layers that would act as the input for that window, but that function did not output any child handles at all.. I got notepad to output two child windows with it.

What is going on and what am i doing wrong?
Last edited on
Console applications don't usually have a message loop, especially for keyboard input.

It is possible to build application loops with event responders, but no GUI windows, but that would be application specific.

I haven't considered the matter myself, but there might be a way of hooking into the Windows approach to posting content from the clipboard through the host window which presents the console application, but that assumes there is a host window - sometimes a 'console' application is executed without one.


Hmm alright, ye seems really weird that it wouldn't be able to simulate keyboard inputs toh.. I were able to set the actual caption using

SendMessage(hwnd, WM_SETTEXT, 0, L"random");

if i recall, something along those lines atleast and that would change the window name from "COM3 - Tera Term VT" to "random".

This is how the application looks, i do not know if it helps but it is almost like a bios window, only that it is an application: https://imgur.com/a/Ka1qkgU

I also tried to play around with the spy tool from visial studio to locate hidden handles, but that did not work either. Seems like it is only one handle which is the main window itself retrieved from

FindWindowA(NULL, "COM3 - Tera Term VT")
-------------------------------------------------------------------------------------------------
Edit:
Also, this block of code outputs 1 aswell, so the PostMessage is being processed..


BOOL k = PostMessage(hwnd, WM_KEYDOWN, VK_DOWN, 0);
cout << k << endl;

Last edited on
Yes, the window surrounding the console has a caption, so sending that a message would work.

If you're sending keystrokes to that same window, you're probably thinking it is the console application inside the window, but it isn't. That outer frame is the window taking your keyboard messages, and hardly anything is made in that window will respond to them.

The console window is interior to that frame, and it probably doesn't have a window handle to even find. Put another way, there is nothing from "Windows" attached to the console, per se.

They do forward pastes from the clipboard, though.

Ye that makes sense, what i initially thought first but did not want to jump to any conclusions. How does the clipboard method work? Never heard of it or done it.

Is it basically opening a process handle to the process and injecting clipboard into it or what is the deal?

Thanks!
I don't know if it still works but last time I coded in VS you could make a console application and add the windows libraries to it and do anything you wanted ... like spawn a file open dialog box or an 'press ok' button box or any of the other built in stuff, and more. You can also make it consoleless and run in the tray, etc. But you want a message loop, you either have to DIY or you can cheat and make a GUI frame instance that has one and just has the show_window flag set to false. I am not sure if those things are 'hackery' and abuse of the tools or valid. I just know they worked way back when and did what I needed to do at that time.
Last edited on
@jonnin - that's on point and correct, but I sense the OP is trying to send messages to existing console applications for which source isn't available.
oh, are you asking for a bot? they make bots that can send keyboard to programs for you, even console programs. That is another story, but its been done to death by the game-cheating people out there. I did it years ago and my program could take joystick/controller input and type into notepad what button was pressed. You can tell windows that a keyboard event was pressed for any program, even non message loop ones, or you could back when. Surely you still can.
Last edited on
I am well aware of "do-nothing" cheats for games that just automate everything!:)
These use the PostMessage / SendMessage function instead of keybd_event as keybd_event requires sync with SetForegroundWindow or just having the window open all the time. The problem with Tera Term VT which is the console program i am trying to simulate actions towards, the console source window does not seem to have a handle, just the actual windows header.

If you jonnin, have any solutions in order to obtain that console window, you are very welcome to do so^^. I just cannot find it, and i do not think it exists. Tried with EnumChildWindows and everything, spy tool from vs, nothing :/

Also, i am not making a bot, i am trying to automate an installation for a network provider, but having the PostMessage works means it opens up for multithreaded installations rather than just doing one at a time. Just a disclaimer.
Last edited on
@wille480,

I have a thought for you to research.

Back in the day, before Windows95, when keystrokes were provided to applications in DOS, we hooked into the interrupt table to intercept or emulate the keyboard actions.

I have no thought that such a technique is in any way applicable, but a modern version might.

Instead of making a generalized Windows application, perhaps what you need to research is the notion of making a "launcher" for the console application, which runs the target.

My thinking is that there may be a way to intercept and emulate keystrokes by a console application, limited to that console window.

If such a console application then launched the target as a "child" process, then all of your intention might be served from inside a single console window which first runs your application, and your application in turn runs the target program while hooking into the keyboard from inside that console window.

If you jonnin, have any solutions in order to obtain that console window, you are very welcome to do so^^. I just cannot find it, and i do not think it exists. Tried with EnumChildWindows and everything, spy tool from vs, nothing :/


if all this stuff works like it did some years ago, you can try making a windows program, spawning a console window in that, and hiding the window. The window will handle the events and you can pass it around from there. Of course, its not really a console program at that point.

It may not work like that anymore. The .net / managed era changed some of the guts and this idea predates that. But a lot of things do work the way they did... its something you could try.

you can also lift the keyboard as said above but if your program is not the one in focus you could be getting the user's grocery list from notepad if you can't find a way to tell who owns the keyboard :)

There must be a way to do this. I can't see, as backwards as microsoft has become the last few years, them removing the ability to mix and match consoles & gui. The question is how to do it in 2020, which I do not know.
Last edited on
Alright haha ty. Ye i'll try. Thanks
Topic archived. No new replies allowed.