How to use PeekMessage to check ALL messages in the queue, without removing them

Viewed 414

This seems simple, but I'm pretty sure it's impossible.

I want to check all messages in the WM queue at a given time, but I do not want to process any message (PM_NOREMOVE). The problem is if I:

while PeekMessage(msg, 0, 0 PM_NOREMOVE)
     # do something with msg

I'll get stuck there forever.

Is there a solution to this?

1 Answers

Not exactly.

PeekMessage allows you to look at the message and either squash it or dispatch it yourself, but there's no way to look at anything other than the first message on the queue.

This is by design - the queued messages need to be processed in the order they are received.

However, what you can do is look at all queued messages before dispatching them which may well allow you to do what you want but requires a different code structure.

Bear in mind that only messages in your queue will be seen, a message that's directly dispatched using SendMessage will not be in the queue so you won't see it - for that you may want to use a Windows Hook or something like that.

Related