Blog
No Gui Gets Mouse Events Right
Date: 21/12/2004
When you write a GUI library it's a matter of time before you need to track the mouse beyond the borders of your own windows. And in general you want to do this using "Mouse Capture" where by all mouse events are sent to the window that has captured the mouse for a short period. Classical examples of this are push buttons and check boxes.

The problem comes when you don't want to capture the mouse, but you still want all mouse events. Now windows (thoughtfully) has a mouse hook procedure that you can setup to capture these events and do various things. But in vanilla mode it only works when the mouse is over your windows, and not only that, when it's over the _client area_ of your windows. So basically it's pointless. For catching clicks outside your window. So what to do? Well it appears that there is a "low level" mode that the mouse hook can operate in. Great! I thought. And sure enough it works. You get all the events, everywhere. So it's sorted isn't it.

Well, um. No actually.

After I made this change for the rest of the afternoon I noticed that the cursor was behaving rather irractically, often "freezing" for longish periods of time (e.g. 2000ms). And sure enough, switch the Lgi mouse hook back to normal mode and the mouse freezing went away. In low level mode you must have a functioning event loop all the time otherwise the cursor will freeze. Charming. In a lot of my apps I tolerate the event loop locking up for a second or so. The user however won't tolerate the cursor locking up all the time so I had to come up with another solution.

Well since none of the built in mouse hook work to my liking I created a thread that sits there polling the mouse every 50ms while I need events. I bit of overkill but it'll work fairly well. And as an added bonus it'll be very cross platform.

Speaking of other platforms, X11 doesn't even have a mouse hook function so you have to poll it anyway. Windows 1 (for trying), Linux 0.

A GUI should really let windows just have a flag "Want mouse events outside my area". Which gives you all mouse events, everywhere, all the time. I've been thinking about writing a GUI one day.

One day.

PS I wrote one in protected mode DOS using VESA that supported overlapped windows, menus, true colour etc... but that was a long time ago. :)
 
Reply
From:
Email (optional): (Will be HTML encoded to evade harvesting)
Message:
 
Remember username and/or email in a cookie.
Notify me of new posts in this thread via email.
BBcode:
[q]text[/q]
[url=link]description[/url]
[img]url_to_image[/img]
[pre]some_code[/pre]
[b]bold_text[/b]