Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add pygame.mouse.get_desktop_pressed #3101

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

damusss
Copy link
Contributor

@damusss damusss commented Sep 15, 2024

This PR follows the steps of #3070 .

That function gets the global position, this one gets the global press state.
The other function is more "urgent" in my opinion, but I'd love to see them both, at the end of the day it's just a wrapper around SDL_GetGlobalMouseState.

Why/Usecase

  • It exists in SDL2, it's an information that is accessible, why keep it a secret
  • It's such a simple wrapper that this PR only adds 53 lines, meaning it's not a big change
  • Practical usecase: let's say you have an application with some UI inside an always on top window, using get_pressed, the first time the user clicks on the window when it was unfocused the return is going to be False because the window has no focus. it will only be True when the window has focus, meaning the user will have to double click the UI to register a click. Using get_desktop_pressed, the click will be registered immediately without the need of a secondary click. On an always-on-top window one can only use get_desktop_pressed and check for the window hover accordigly.

Sample code

import pygame

win = pygame.Window("desktop pressed test", (200, 200))
win.always_on_top = True

# if a member of the SC is reading this, Window.surface >>>> Window.get_surface!
screen = win.get_surface()

clock = pygame.Clock()

while True:
    for event in pygame.event.get():
        if event.type == pygame.WINDOWCLOSE and event.window == win:
            pygame.quit()
            raise SystemExit

    screen.fill(0)

    dmouse = pygame.mouse.get_desktop_pressed()[0]
    mouse = pygame.mouse.get_pressed()[0]
    if mouse and dmouse:
        print("Pressing LEFT with focus")
    elif dmouse:
        print("Pressing LEFT without focus")

    # click other windows, click the taskbar, you'll always get the left button pressed

    # also test this: after clicking on another window,
    # hold the mouse on the window. You'll see it says "pressing without focus" until you click again

    win.flip()
    clock.tick(60)

@damusss damusss added New API This pull request may need extra debate as it adds a new class or function to pygame mouse pygame.mouse labels Sep 15, 2024
@damusss damusss requested a review from a team as a code owner September 15, 2024 16:18
@ankith26
Copy link
Member

This PR has gotten me thinking about the API design for this PR and the other one. Why are we adding two functions when this could be a keyword argument of the existing functions very easily? The semantics and signatures of the "desktop" variants are very similar or same as the "non desktop" variants

@damusss
Copy link
Contributor Author

damusss commented Sep 16, 2024

This PR has gotten me thinking about the API design for this PR and the other one. Why are we adding two functions when this could be a keyword argument of the existing functions very easily? The semantics and signatures of the "desktop" variants are very similar or same as the "non desktop" variants

how exactly do you think api-wise a keyword argument could "switch" from a 5tuple to a 2tuple or both? because I thought of combining it aswell but i had the plain mouse state return in mind which wouldn't be consistent

@damusss
Copy link
Contributor Author

damusss commented Sep 16, 2024

Hold on I might have understood what you mean, you intend on adding a desktop keyword argument to get_pos() and get_pressed(), I feel so dumb. It's lowkey a 🔥 idea, so I'll implement it so you can see what you prefer.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
dont merge mouse pygame.mouse New API This pull request may need extra debate as it adds a new class or function to pygame
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants