Ejemplo n.º 1
0
    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()
Ejemplo n.º 2
0
    def set_mouse_position(self, x, y, absolute=False):
        if not absolute:
            rect = RECT()
            _user32.GetClientRect(self._view_hwnd, byref(rect))
            _user32.MapWindowPoints(self._view_hwnd, HWND_DESKTOP, byref(rect), 2)

            x = x + rect.left
            y = rect.top + (rect.bottom - rect.top) - y

        _user32.SetCursorPos(x, y)
Ejemplo n.º 3
0
    def _reset_exclusive_mouse_screen(self):
        """Recalculate screen coords of mouse warp point for exclusive
        mouse."""
        p = POINT()
        rect = RECT()
        _user32.GetClientRect(self._view_hwnd, byref(rect))
        _user32.MapWindowPoints(self._view_hwnd, HWND_DESKTOP, byref(rect), 2)
        p.x = (rect.left + rect.right) // 2
        p.y = (rect.top + rect.bottom) // 2

        # This is the point the mouse will be kept at while in exclusive
        # mode.
        self._exclusive_mouse_screen = p.x, p.y
Ejemplo n.º 4
0
    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))
Ejemplo n.º 5
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:
            # 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)
Ejemplo n.º 6
0
    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)