Esempio 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()
Esempio n. 2
0
 def get_location(self):
     rect = RECT()
     _user32.GetClientRect(self._hwnd, byref(rect))
     point = POINT()
     point.x = rect.left
     point.y = rect.top
     _user32.ClientToScreen(self._hwnd, byref(point))
     return point.x, point.y
    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)
    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
Esempio n. 5
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))
Esempio n. 6
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)
Esempio n. 7
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)
Esempio n. 8
0
 def get_location(self):
     rect = RECT()
     _user32.GetClientRect(self._hwnd, byref(rect))
     _user32.ClientToScreen(self._hwnd, byref(rect))
     return rect.left, rect.top