def set_exclusive_mouse(self, exclusive=True): if self._exclusive_mouse == exclusive and \ self._exclusive_mouse_focus == self._has_focus: return if exclusive and self._has_focus: # Move mouse to the center of the window. self._reset_exclusive_mouse_screen() x, y = self._exclusive_mouse_screen self.set_mouse_position(x, y, absolute=True) # 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)) else: # Release clip _user32.ClipCursor(None) self._exclusive_mouse = exclusive self._exclusive_mouse_focus = self._has_focus self.set_mouse_platform_visible()
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 not exclusive: 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: self._update_clipped_cursor() else: # Release clip _user32.ClipCursor(None) self._exclusive_mouse = exclusive self._exclusive_mouse_focus = self._has_focus self.set_mouse_platform_visible(not exclusive)
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: # Move mouse to the center of the window. self._reset_exclusive_mouse_screen() x, y = self._exclusive_mouse_screen self.set_mouse_position(x, y, absolute=True) # 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)) else: # Release clip _user32.ClipCursor(None) # Move mouse bak to the middle of the client area. if self._exclusive_mouse_screen: x, y = self._exclusive_mouse_screen self.set_mouse_position(x, y, absolute=True) self._exclusive_mouse = exclusive self._exclusive_mouse_focus = self._has_focus self.set_mouse_platform_visible(not exclusive)
def set_exclusive_mouse(self, exclusive=True): if self._mouse_exclusive == 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) super().set_exclusive_mouse(exclusive) self._exclusive_mouse_focus = self._has_focus self.set_mouse_platform_visible(not exclusive)
def _update_clipped_cursor(self): # Clip to client area, to prevent large mouse movements taking # it outside the client area. if self._in_title_bar or self._pending_click: return rect = RECT() _user32.GetClientRect(self._view_hwnd, byref(rect)) _user32.MapWindowPoints(self._view_hwnd, HWND_DESKTOP, byref(rect), 2) # For some reason borders can be off 1 pixel, allowing cursor into frame/minimize/exit buttons? rect.top += 1 rect.left += 1 rect.right -= 1 rect.bottom -= 1 _user32.ClipCursor(byref(rect))