Sign in to follow this  

WindowProc in c++ Console

This topic is 3573 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Is it possible to add a WindowsProc to a c++ console application? I am trying to handle input in a console application and need to continue doing processes and if a key is pressed possibly change values based on the key being pressed. It is much like a game loop in a Windows Application. Hopefully that explains what I'm trying to do. Anything that will point me in the right direction would be much appreciated.

Share this post


Link to post
Share on other sites
You can't really get a window proc for the console window (Well, you could get its HWND and subclass it), but you can easily emulate it. Instead of calling your window message pump once per tick, just call your own function. Your own function can check what keys are pressed (if any) using the various Win32 functions, or the C/C++ keyboard input functions.

If you want this to all go on without you having to call your "window pump" function, you could do it in a thread instead - although that'll be more complicated.

Share this post


Link to post
Share on other sites
Couple of options:
  1. Add a console window to a Windows GUI app using AllocConsole.

  2. Add a message pump to your console application PostThreadMessage, but note that messages sent via this pump can not (generally) be dispatched via DispatchMessage, as they do not belong to a window.

  3. Simulate the console window in a regular Win32 window. Most work, most flexibility.

Share this post


Link to post
Share on other sites
Quote:
Original post by mperry
Is it possible to add a WindowsProc to a c++ console application? I am trying to handle input in a console application and need to continue doing processes and if a key is pressed possibly change values based on the key being pressed. It is much like a game loop in a Windows Application. Hopefully that explains what I'm trying to do. Anything that will point me in the right direction would be much appreciated.


Hi,

This API function *might* be another option also. I say might, because I don't know if this function blocks / waits if there is no input in the buffer. My assumption however would be that it would do what you would expect a peek function to do and return immediately, even if there is no input.

If you could get this working you could carry on with your background tasks and only read from the input buffer once there is something there to read- preventing your code from grounding to a halt.

Worth a shot maybe..

Share this post


Link to post
Share on other sites
thk u all for your quick responses. Its been a while since I played with the console. I think I may have worded what I was trying to accomplish wrong. However, I have found a function in conio that will help me do what I want. I can use the function kbhit() to see if a character on the keyboard was pressed, if this is false I can go on with my business, if it is true I can respond to the corresponding keypress, then continue on. Sorry for making such a simple problem so complicated. Again, thanks for all of your quick responses.

Share this post


Link to post
Share on other sites
Quote:
Original post by mperry
thk u all for your quick responses. Its been a while since I played with the console. I think I may have worded what I was trying to accomplish wrong. However, I have found a function in conio that will help me do what I want. I can use the function kbhit() to see if a character on the keyboard was pressed, if this is false I can go on with my business, if it is true I can respond to the corresponding keypress, then continue on. Sorry for making such a simple problem so complicated. Again, thanks for all of your quick responses.


You can also use the Win32 function GetKeyStateAsync to poll whether a certain key is being pressed.

Share this post


Link to post
Share on other sites

This topic is 3573 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this