WinAPI Keyboard Input

Started by
2 comments, last by og knuckles 8 years, 7 months ago

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

Advertisement
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?

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.

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.

This topic is closed to new replies.

Advertisement