Skip to main content
GameDev.net gamedev.net
🔒 Locked

WindowProc in c++ Console

Started by mperry Mar 7, 2008 at 1:20 PM 4 replies 2.8k views
Original Post
mperry
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.
Evil Steve
Evil Steve
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.
Oluseyi
Oluseyi
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.

Darragh
Darragh
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..
mperry
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.
MJP
MJP
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.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.