0

I made a small application for myself that is running how it should, but I cant for the life of me figure out how to allow/force it to accept input when not the window in focus. The program counts the presses of one of the buttons on my gamepad and updates the count on the window, but both the game and my application need to be in focus to recieve input. I made this in pygame as it allowed me to map the gamepad button easily, but if anyone knows a way to make it functionally the same while allowing the game and the application to record at the same time, it would be appreciated

import pygame
from sys import exit

pygame.init()
j = pygame.joystick.Joystick(0)
j.init()

count = 0

#Making the Window
display_surface = pygame.display.set_mode((350, 200))
pygame.display.set_caption('Reset Counter')
font = pygame.font.Font('freesansbold.ttf', 32)
text = font.render(("Reset Count: "+str(count)), True, (255, 0, 0))
textRect = text.get_rect()
textRect.center = (350 // 2, 200 // 2)
display_surface.fill((255, 255, 255))
display_surface.blit(text, textRect)
pygame.display.update()

while True:
    events = pygame.event.get()
    pygame.event.set_grab(False)
    for event in events:
        #Looking for button 15 on my Gamepad
        if event.type == pygame.JOYBUTTONDOWN:
            if j.get_button(15):
                count = count + 1
                #Updating Text
                text = font.render(("Reset Count: "+str(count)), True, (255, 0, 0))
                display_surface.fill((255, 255, 255))
                display_surface.blit(text, textRect)
                pygame.display.update()
        #Functional Close Button
        elif event.type == pygame.QUIT:
            pygame.quit()
            exit()
Rabbid76
  • 202,892
  • 27
  • 131
  • 174
Talisman
  • 1
  • 1
  • 3
    I think this post will help. https://stackoverflow.com/questions/31555665/get-inputs-without-focus-in-python-pygame – nuke1115 Jan 17 '23 at 15:58

1 Answers1

0

You have to set to set SDL_HINT_JOYSTICK_ALLOW_BACKGROUND_EVENTS before Pygame and the joystick module is initialized:

import os
os.environ["SDL_JOYSTICK_ALLOW_BACKGROUND_EVENTS"] = "1"

import pygame
pygame.init()
Rabbid76
  • 202,892
  • 27
  • 131
  • 174