Sign in to follow this  

WinAPI Keyboard Input

This topic is 824 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

Currently im working on a WINAPI keyboard input event-driven system. With not much success I am not fluent in win API so im sure I am either not using it correctly or I am missing something...possibly both tongue.png

#define _WIN32_WINNT 0x0500
#include "windows.h"



int main()
{
	MSG msg;
	//HWND consoleWindow = GetForegroundWindow();
	while (true)
	{
		if (PeekMessage(&msg, NULL, WM_KEYFIRST, WM_KEYLAST, PM_REMOVE))
		{
			TranslateMessage(&msg);
			DispatchMessage(&msg);
		}
	}



	return 0;
}

obviously while true is just for debug purposes. So what I am trying to do is pull the applications messages from first key to last. However this code never moves pass the PeekMessage if statement. I have tried passing the consoleWindow handle as well and still nothing.

 

-this is a command Line window

-I am trying to get every key that is being pressed not just the last one so if I press 10 keys at once I need to be able to see 10 instances of KEYDOWN 

Edited by og knuckles

Share this post


Link to post
Share on other sites
You don't appear to be creating a window at all. You won't receive practically all messages without a fully created window (that also means you need a message loop which correctly processes at least the WM_CREATE/WM_NC_CREATE-family of messages). I'm not sure about the rules for the console window but I have my doubts if retrieving it via GetForegroundWindow() works. Do you even get a non-NULL handle?

Share this post


Link to post
Share on other sites

You don't appear to be creating a window at all. You won't receive practically all messages without a fully created window (that also means you need a message loop which correctly processes at least the WM_CREATE/WM_NC_CREATE-family of messages). I'm not sure about the rules for the console window but I have my doubts if retrieving it via GetForegroundWindow() works. Do you even get a non-NULL handle?

It does output a non-NULL handle. GetConsoleWindow() also works to return the handle to the window.

 

Can we just clarify, are you trying to create a command line application or a windowed application here?

 

If its a command line application, you don't need to use the message loop like that. Here is an example of a minimal console application using the console API of Win32.

Currently it is command line. I thought about going the inputBuffer route. I am just trying to avoid looping a lot... more like an event-driven listeners. So it only loops if there is input.

Share this post


Link to post
Share on other sites

This topic is 824 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