Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use PeekMessage to retrieve every messages except input (mouse + keyboard)

I have somewhere in my code a loop function that call PeekMessage in order to retrieve events.

Currently it looks like this:

while (PeekMessage(&Message, NULL, 0, 0, PM_REMOVE))
{
    // Process events
}

Now I would like to manage input in a different location, meaning I'd like to retrieve messages such as WM_KEYDOWN, WM_MOUSEMOVE (mouse and keyboard events) in a different place, at a different time of my main loop.

PeekMessage's third and fourth arguments allow to define a range of message to return, so I could use this, using provided macros WM_KEYFIRST, WM_KEYLAST, WM_MOUSEFIRST and WM_MOUSELAST. But it's unconvenient because I have two ranges to check for input, and therefore three ranges for everything remaining.

The last parameter is a flag and I could pass PM_REMOVE | PM_QS_INPUT for input. But then, what should I pass in the other loop, where I want to get every other messages? There is no PM_QS_EVERYTHING_EXCEPT_INPUT macro...

What would be the most elegant way to do this?

like image 225
Benlitz Avatar asked May 13 '12 01:05

Benlitz


People also ask

What does PeekMessage do?

PeekMessage retrieves messages associated with the window identified by the hWnd parameter or any of its children as specified by the IsChild function, and within the range of message values given by the wMsgFilterMin and wMsgFilterMax parameters.

What is the difference between PeekMessage and GetMessage?

The main difference between the two functions is that GetMessage does not return until a message matching the filter criteria is placed in the queue, whereas PeekMessage returns immediately regardless of whether a message is in the queue.


1 Answers

You could try to use "Filters". Win32 calls that "hooks", take a look to msdn's SetWindowsHookEx function. you've got the possibility to install a thread specific filter for keyboard and mouse messages. Then you can push messages to a queue of your maintenance and access it manually later on (knowing you can remove the related messages from the public Queue for that thread if you chose to, from the hook, so that only your user-queue will have the messages). though, you could simply also directly push to this use-queue directly from the PeekMessage switch in every case that is of interest for you.

like image 86
Lightness1024 Avatar answered Sep 23 '22 21:09

Lightness1024