I'm writing a small Pygame script and I need to know if the mouse has left the Pygame window
I don't know how else to explain it. It seems simple, but I can't find a solution anywhere.
The current position of the mouse can be determined via pygame. mouse. get_pos() . The return value is a tuple that represents the x and y coordinates of the mouse cursor.
You have to call pygame. event. set_grab(True) as well. Better allow the users to exit with the Esc or another key, because they won't be able to click the x button anymore to close the window.
pygame mouse posReturns the x and y position of the mouse cursor. The position is relative to the top-left corner of the display.
pygame.mouse.get_pos()¶ get the mouse cursor position get_pos() -> (x, y) Returns the xand yposition of the mouse cursor. The position is relative to the top-left corner of the display. The cursor position can be located outside of the display window, but is always constrained to the screen. pygame.mouse.get_rel()¶ get the amount of mouse movement
pygame.mouse.get_focused()¶ check if the display is receiving mouse input get_focused() -> bool Returns true when pygame is receiving mouse input events (or, in windowing terminology, is "active" or has the "focus"). This method is most useful when working in a window. By contrast, in full-screen mode, this method always returns true.
There is proper functionality for mouse wheel behaviour with pygame 2 supporting pygame.MOUSEWHEELevents. The new events support horizontal and vertical scroll movements, with signed integer values representing the amount scrolled (xand y), as well as flippeddirection (the set positive and negative values for each axis is flipped).
pygame.MOUSEWHEELtype of an event. When this event is triggered, a developer can access the appropriate Eventobject with pygame.event.get(). The object can be used to access data about the mouse scroll, such as which(it will tell you what exact mouse device trigger the event). Code example of mouse scroll (tested on 2.0.0.dev7)¶
pygame.mouse.focus()
gives 0
when mouse leaves window (at least in 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()
I don't understand why there is no one tell you to use ACTIVEEVENT.
I don't know in older pygame version, but in windows 10, python 3.7.3, pygame 1.9.6, I can use this:
import pygame as pg
video = pg.display.set_mode((300,300))
running = True
while running:
for event in pg.event.get():
if (event.type == pg.ACTIVEEVENT):
if (event.gain == 1): # mouse enter the window
print("Welcome, cursor! :D ",
"Selamat datang, kursor! :D")
else: # mouse leave the window
print("Good bye, cursor! :( ",
"Sampai jumpa, kursor! :(")
elif (event.type == pg.QUIT):
running = False
pg.display.quit()
print("Bye! Have a nice day! ",
"Sampai jumpa! Semoga harimu menyenangkan! :)")
I've tested it. It works!
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With