I'm trying to implement a traditional 3D camera on Mac, by grabbing and hiding the cursor, and using DeviceEvent::MouseMotion to track movement. However, I can't figure out a way to capture mouse button clicks in this scenario.
DeviceEvent::Button does not fire at all for mouse button clicks on Mac. Is this the expected behaviour?
WindowEvent::MouseInput does fire, but only if the mouse was inside the area of the window before capturing the cursor. And if the mouse was outside the window, capturing the cursor doesn't stop the click from falling through to whatever application is in the background.
Any advice on making this work? Or guidelines as to the expected behaviour so that I can patch winit?
As a workaround, moving the cursor to an arbitrary position inside the window seems to work:
window.set_cursor_position((100, 100).into()).unwrap();
window.hide_cursor(true);
window.grab_cursor(true).unwrap();
With the downside that the cursor is now somewhere unexpected to the user when the program exits.
@swiftcoder have you tried it on other platforms?
It looks like DeviceEvent::Button isn't implemented on macOS. From searching the code it looks like it should be implemented in this function, and I don't know why it hasn't been implemented yet. Should be a pretty simple PR to make, though!
Looking through the code, I don't think DeviceEvent::Button is implemented on Windows either?
It's implemented here. For some reason, a lot of the Windows events are imported by the event's variant, instead of used through the event - that's why it was hard to find this.
I've added logging to the cursor_grab example in #967. Unfortunately it highlights that event intercepting at the [NSApplication sendEvent:] level doesn't capture mouse clicks outside of the window.
I think the correct fix here is to automatically move the cursor to an arbitrary position inside the window when set_cursor_grab(true) is called, and reset to the original position when set_cursor_grab(false) is called (or the program exits)?
Or I guess, follow the guide linked in the actual source code :)
Should this issue be marked as resolved? It seems like the initial bug (DeviceEvent::Button not firing) was fixed in #967. There is still the TODO about better grabbing, but I imagine that's not what is holding this up.
@markhildreth Thanks for bringing that up! This issue indeed has been fixed. I'll go ahead and open an issue for better grabbing support, since we don't seem to have that yet.
EDIT: #1093