def _event_mousebutton(self, ev, button, lParam): if ev == 'on_mouse_press': _user32.SetCapture(self._view_hwnd) else: _user32.ReleaseCapture() x, y = self._get_location(lParam) y = self._height - y self.dispatch_event(ev, x, y, button, self._get_modifiers()) return 0
def set_exclusive_mouse(self, exclusive=True): if self._exclusive_mouse == exclusive and \ self._exclusive_mouse_focus == self._has_focus: return # Mouse: UsagePage = 1, Usage = 2 raw_mouse = RAWINPUTDEVICE(0x01, 0x02, 0, None) if exclusive: raw_mouse.dwFlags = RIDEV_NOLEGACY raw_mouse.hwndTarget = self._view_hwnd else: raw_mouse.dwFlags = RIDEV_REMOVE raw_mouse.hwndTarget = None if not _user32.RegisterRawInputDevices( byref(raw_mouse), 1, sizeof(RAWINPUTDEVICE)): if exclusive: raise WindowException("Cannot enter mouse exclusive mode.") self._exclusive_mouse_buttons = 0 if exclusive and self._has_focus: # Clip to client area, to prevent large mouse movements taking # it outside the client area. rect = RECT() _user32.GetClientRect(self._view_hwnd, byref(rect)) _user32.MapWindowPoints(self._view_hwnd, HWND_DESKTOP, byref(rect), 2) _user32.ClipCursor(byref(rect)) # Release mouse capture in case is was acquired during mouse click _user32.ReleaseCapture() else: # Release clip _user32.ClipCursor(None) self._exclusive_mouse = exclusive self._exclusive_mouse_focus = self._has_focus self.set_mouse_platform_visible(not exclusive)