Noitidart
Noitidart

Reputation: 37238

My message loop in thread is not getting any messages

I have created a thread, I want to listen to mouse messages my app receives.

However PeekMessage is never returning true. I even tried min and max filter of 0.

Here is my message loop:

PeekMessage(LMessage, NULL, 0, 0, PM_NOREMOVE);

while (true) {
     var rez = PeekMessage(LMessage, NULL, 0, 0, PM_REMOVE)
     if (rez) {
          // console.log('peekmessage true');
     }
     Sleep 1000;
}

// console.log('message loop eneded');

As my hwnd is NULL i thought I should receive all messages to any window in my app, Im not getting anything though. Does anyone know whats up?

I also tried GetMessage approach:

var rez = GetMessage(LMessage, NULL, 0, 0);
console.log('rez:', rez);

However it just hangs and never gets to the console.log.

Thanks

Upvotes: 0

Views: 614

Answers (1)

theB
theB

Reputation: 6738

The documentation for PeekMessage says:

If hWnd is NULL, PeekMessage retrieves messages for any window that belongs to the current thread, and any messages on the current thread's message queue whose hwnd value is NULL.

[Emphasis Added]

A similar note is in the GetMessage documentation.

The call to GetMessage stalls because there is no message queue for the thread, and no messages to wait for, so it will wait forever.

If you need window messages in your thread, have the thread create its own message-only window and use that as the target for raw input. To create a message-only window call CreateWindowEx with hwndParent set to HWND_MESSAGE.

Otherwise, you'd need to forward messages from your main message loop using PostThreadMessage.

Upvotes: 2

Related Questions