Skip to content

Fix windowevent and key repeat in sdl2-compat #3470

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

Open
wants to merge 2 commits into
base: main
Choose a base branch
from

Conversation

ankith26
Copy link
Member

@ankith26 ankith26 commented Jun 4, 2025

WINDOWEVENT translation was broken when compiled with sdl2-compat.

The issue was in our _pg_translate_windowevent function. The current code was directly modifying the event type there but it seems like it is simply an implementation detail that we were relying on, and one isn't supposed to actually modify events in SDL_FilterEvents based filtering. Plus, this is easier to fix on our side than fix on sdl2-compat side.

I also took the opportunity to improve the comments while I was giving the implementation a good think.

This fixes a failing unit test so I don't see the need to add more tests here.

UPDATE: key repeat was broken and for similar reasons, I have fixed that here as well.

@ankith26 ankith26 requested a review from a team as a code owner June 4, 2025 16:46
@ankith26 ankith26 force-pushed the ankith26-windowevent-fix branch from eaa6ee8 to 3116fe6 Compare June 4, 2025 16:54
@ankith26 ankith26 changed the title Fix windowevent translation in sdl2-compat Fix windowevent and key repeat in sdl2-compat Jun 4, 2025
@ankith26 ankith26 force-pushed the ankith26-windowevent-fix branch from 54cd49c to b5843fc Compare June 4, 2025 19:18
@ankith26
Copy link
Member Author

ankith26 commented Jun 4, 2025

from @Starbuck5 in #3287

In python -m pygame.examples.video neither of the two windows can be exited by clicking the x button. (Probably related to whatever it didn't like about the event watch?)

This PR should fix the issue.

@ankith26 ankith26 force-pushed the ankith26-windowevent-fix branch from b5843fc to 36e0989 Compare June 5, 2025 05:29
@ankith26
Copy link
Member Author

ankith26 commented Jun 5, 2025

import pygame
import sys

pygame.init()
screen = pygame.display.set_mode((400, 200), pygame.RESIZABLE)
pygame.display.set_caption("Key Repeat & Block Test")
clock = pygame.time.Clock()
font = pygame.font.Font(None, 36)

counter = 0
key_repeat_on = True
key_blocked_on = False

pygame.key.set_repeat(300, 100)

running = True
while running:
    screen.fill((30, 30, 30))

    text = font.render(f"Counter: {counter}", True, (255, 255, 255))
    info1 = font.render(f"Repeat: {'ON' if key_repeat_on else 'OFF'}", True, (180, 180, 180))
    info2 = font.render(f"Blocked: {'YES' if key_blocked_on else 'NO'}", True, (180, 180, 180))

    screen.blit(text, (50, 40))
    screen.blit(info1, (50, 90))
    screen.blit(info2, (50, 130))
    
    pygame.display.flip()

    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            running = False

        elif event.type == pygame.KEYDOWN:
            print(f"KEYDOWN: {pygame.key.name(event.key)}")

            if event.key == pygame.K_SPACE:
                counter += 1

            elif event.key == pygame.K_r:
                key_repeat_on = not key_repeat_on
                if key_repeat_on:
                    pygame.key.set_repeat(300, 100)
                else:
                    pygame.key.set_repeat()

        elif event.type == pygame.MOUSEBUTTONDOWN:
            key_blocked_on = not key_blocked_on
            if key_blocked_on:
                pygame.event.set_blocked(pygame.KEYDOWN)
            else:
                pygame.event.set_allowed(pygame.KEYDOWN)

        elif event.type >= pygame.WINDOWSHOWN and event.type <= pygame.WINDOWDISPLAYCHANGED:
            print(event)

    clock.tick(60)

pygame.quit()
sys.exit()

test script for this PR

@ankith26 ankith26 added this to the 2.5.6 milestone Jun 7, 2025
@Starbuck5
Copy link
Member

I tested with your script and SDL2 compat and key repeat doesn't seem to work.

@Starbuck5
Copy link
Member

from @Starbuck5 in #3287

In python -m pygame.examples.video neither of the two windows can be exited by clicking the x button. (Probably related to whatever it didn't like about the event watch?)

This PR should fix the issue.

It doesn't in my testing. I'm using the latest SDL 2.32.56 release from them.

@ankith26
Copy link
Member Author

hmm, it works for me. Are you sure you are on this branch? Also, the r key toggles key repeat in the code, just in case you missed it.

@Starbuck5
Copy link
Member

image

pygame-ce 2.5.6.dev1 (SDL 2.32.56, Python 3.9.5)

Copy link
Member

@Starbuck5 Starbuck5 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Okay on further testing it actually does work, I was having an unknown problem getting the code to update in my site-packages directory, even though the timestamps updated apparently the code wasn't. I had to delete a few cache directories to get everything building properly again.

@Starbuck5 Starbuck5 added the event pygame.event label Jun 29, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
event pygame.event
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants