Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Detect if mouse has left Pygame window

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.

like image 612
AlgoRythm Avatar asked Nov 18 '16 01:11

AlgoRythm


People also ask

How do I know if my mouse is down in Pygame?

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.

How do you lock the mouse in Pygame?

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.

What does Pygame mouse Get_pos () do?

pygame mouse posReturns the x and y position of the mouse cursor. The position is relative to the top-left corner of the display.

How to get the mouse position in Pygame?

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

How do I check if the display is receiving mouse input?

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.

Does Pygame 2 support mouse wheel events?

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).

How to get the mouse scroll event type in Pygame?

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)¶


2 Answers

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()
like image 169
furas Avatar answered Nov 05 '22 02:11

furas


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!

like image 40
Hzzkygcs Avatar answered Nov 05 '22 02:11

Hzzkygcs