Jump to content
  • Advertisement
Sign in to follow this  
theZapper

Mouse input in a different thread

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

Hello I'm trying to use a different thread than the main window thread to get mouse input. Problem is that (on Windows) mouse messages are only sent to the thread that creates the window. How do I tell the window to send mouse events to a different thread, or how do I get the other thread to collect events from the main thread? Cheers

Share this post


Link to post
Share on other sites
Advertisement
Sorry, I'm using C++ and the standard Windows threading API.

I realised you can specify a window handle in GetMessage() so I did that and gave it the handle, but it still won't seem to get the Mouse events.

In the main thread I have:


while(run)
{
ret = GetMessage(&msgKeyboard, NULL, WM_KEYFIRST, WM_KEYLAST ); // keyboard

if (ret == -1)
{
// error
}
else if(!TranslateAccelerator(msgKeyboard.hwnd, hAccelTable, &msgMouse))
{
TranslateMessage(&msgKeyboard);
DispatchMessage(&msgKeyboard);
}
drawingDriver->render();
}


and in the other thread class I have:

while(runLoop)
{
ret = GetMessage(&msgMouse, m_WindowHandle, WM_MOUSEFIRST, WM_MOUSELAST ); // mouse

// Process mouse events
TranslateMessage(&msgMouse);
DispatchMessage(&msgMouse);
}


But it never seems to get very far in the thread loop.

Share this post


Link to post
Share on other sites
No expert here, but aren't UI events sent to the thread that created the UI object (in your case the window). To my understanding, that's how it works in .NET, and that just wraps thw Win32 API. Have you tried creating your window in the thread you want the mouse messages on?

Share this post


Link to post
Share on other sites
You've pretty much just repeated my first post!

It work's fine colleting the messages from the main thread, it's currently getting keyboard messages from there. And now I have the Mouse thread asking for messages from the correct window by using the window handle in the GetMessage function, but it still doesn't want to deliver them there.

Share this post


Link to post
Share on other sites
Quote:
Original post by theZapper
You've pretty much just repeated my first post!

It work's fine colleting the messages from the main thread, it's currently getting keyboard messages from there. And now I have the Mouse thread asking for messages from the correct window by using the window handle in the GetMessage function, but it still doesn't want to deliver them there.
He is also repeating the documentation for GetMessage:
Quote:
hWnd
[in] Handle to the window whose messages are to be retrieved. The window must belong to the current thread.

However, neither is strictly correct - you can use AttachThreadInput to allow another thread to get messages for a window, but you have to be careful because it can cause serious problems if you don't understand all the implications (which means understanding certain windows internals which are not in MSDN to the best of my knowledge - search microsoft blogs for the function name as a start) and is generally best avoided.

Share this post


Link to post
Share on other sites
I'm not sure how I was repeating what you said. I'm also not sure the idea would work, but I wasn't saying have the mouse thread try to get messages by passing in the handle of the window you're trying to capture events on. I said create the window in the same thread that you then try to get mouse events in, a thread which is not the main thread.

It seems though that isn't what you need anyway, so sorry if you lost any time on it. I did want to clarify that I wasn't just restating your problem.

Share this post


Link to post
Share on other sites
If i remember correctly, when i programmed WinAPI, whenever i called GetMessage specifying the handle to the window, it NEVER worked for me. To fix it i just passed in NULL as the handle and it worked.

I hope this helps.

Share this post


Link to post
Share on other sites
Ahhh...

"hWnd
[in] Handle to the window whose messages are to be retrieved. The window must belong to the current thread."


Seems to be the crucial part I'd missed. I'm creating the window in the main thread, which means the window belongs to that thread. Then I'm creating another thread and trying to get the messages for a window which doesn't belong to the current thread.

b1gjo3, that's probably why your code never worked, and why mine doesn't either.

Thanks Extrarius.

So then, how can I set up a different thread to catch input on a window not in the current thread? It must be possible.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!