检测鼠标是否离开 Pygame 窗口

Alg*_*thm 4 python mouse pygame

我正在编写一个小的 Pygame 脚本,我需要知道鼠标是否离开了 Pygame 窗口

我不知道还能怎么解释。看起来很简单,但我在任何地方都找不到解决方案。

fur*_*ras 7

pygame.mouse.focus()0当鼠标离开窗口时给出(至少在 Linux 中)

#!/usr/bin/env python3

import pygame

pygame.init()
screen = pygame.display.set_mode((800,600))

is_running = True
while is_running:

    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            is_running = False
        elif event.type == pygame.KEYDOWN:
            if event.key == pygame.K_ESCAPE:
                is_running = False

    print(pygame.mouse.get_focused())

pygame.quit()
Run Code Online (Sandbox Code Playgroud)