'How to make my Pygame window always active and listening for input?
I spent the last 3 hours searching for a way to create a simple toggle button script that lets me know if a button on my Thrustmaster HOTAS has been pressed. As I am a bloody beginner I didn't write this code myself. I found one that suits my means and configured it to my liking.
Now I want this script to be active and running when I am ingame. Preferably also on top of other windows. Because it doesn't recognize the inputs if the window isn't active.
Bring a pygame window to front
How to make python window run as "Always On Top"?
Pygame set window on top without changing its position
These were the most promising ones I found beside many others. Tried all of them even though the second one is for linux.
pygame capture keyboard events when window not in focus
Also found this one, but they explain how to hook Keyboard inputs, not Joystick inputs.
I am using Win10 and the newest python version.
Update: I added these lines and now the window stays on top all the time but it's still not always active.
import win32gui
import win32con
hwnd = win32gui.GetForegroundWindow()
win32gui.SetWindowPos(hwnd,win32con.HWND_TOPMOST,100,100,200,200,0)
That's the whole script I am using
import sys
import pygame
import win32gui
import win32con
from pygame.locals import *
pygame.init()
pygame.display.set_caption('ToggleCheck')
screen = pygame.display.set_mode((180, 110), 0, 32)
hwnd = win32gui.GetForegroundWindow()
win32gui.SetWindowPos(hwnd,win32con.HWND_TOPMOST,100,100,200,200,0)
clock = pygame.time.Clock()
pygame.mixer.init(frequency=44100, size=32, channels=2, buffer=4096)
pygame.joystick.init()
joysticks = [pygame.joystick.Joystick(i) for i in range(pygame.joystick.get_count())]
for joystick in joysticks:
print(joystick.get_name())
my_square = pygame.Rect(30, 30, 50, 50)
my_square2 = pygame.Rect(100, 30, 50, 50)
my_square_color = 0
my_square_color2 = 0
colors = [(255, 0, 0), (0, 255, 0)]
motion = [0, 0]
while True:
screen.fill((0, 0, 0))
pygame.draw.rect(screen, colors[my_square_color], my_square)
pygame.draw.rect(screen, colors[my_square_color2], my_square2)
for event in pygame.event.get():
if event.type == JOYBUTTONDOWN:
print(event)
if event.button == 8:
if (my_square_color % 2) == 0:
my_square_color = (my_square_color + 1) % len(colors)
on = pygame.mixer.Sound("Desktop\DCS\checker\sounds\on.wav")
pygame.mixer.Sound.play(on)
else:
my_square_color = (my_square_color + 1) % len(colors)
on = pygame.mixer.Sound("Desktop\DCS\checker\sounds\off.wav")
pygame.mixer.Sound.play(on)
if event.type == JOYBUTTONDOWN:
print(event)
if event.button == 9:
if (my_square_color2 % 2) == 0:
my_square_color2 = (my_square_color2 + 1) % len(colors)
on = pygame.mixer.Sound("Desktop\DCS\checker\sounds\on.wav")
pygame.mixer.Sound.play(on)
else:
my_square_color2 = (my_square_color2 + 1) % len(colors)
on = pygame.mixer.Sound("Desktop\DCS\checker\sounds\off.wav")
pygame.mixer.Sound.play(on)
if event.type == JOYDEVICEADDED:
joysticks = [pygame.joystick.Joystick(i) for i in range(pygame.joystick.get_count())]
for joystick in joysticks:
print(joystick.get_name())
if event.type == JOYDEVICEREMOVED:
joysticks = [pygame.joystick.Joystick(i) for i in range(pygame.joystick.get_count())]
if event.type == QUIT:
pygame.quit()
sys.exit()
if event.type == KEYDOWN:
if event.key == K_ESCAPE:
pygame.quit()
sys.exit()
pygame.display.update()
clock.tick(60)
Solution 1:[1]
Try using this py-joystick module https://pypi.org/project/pyjoystick/
from pyjoystick.sdl2 import Key, Joystick, run_event_loop
def print_add(joy):
print('Added', joy)
def print_remove(joy):
print('Removed', joy)
def key_received(key):
print('received', key)
if key.value == Key.HAT_UP:
#do something
elif key.value == Key.HAT_DOWN:
#do something
if key.value == Key.HAT_LEFT:
#do something
elif key.value == Key.HAT_UPLEFT:
#do something
elif key.value == Key.HAT_DOWNLEFT:
#do something
elif key.value == Key.HAT_RIGHT:
#do something
elif key.value == Key.HAT_UPRIGHT:
#do something
elif key.value == Key.HAT_DOWNRIGHT:
#do something
run_event_loop(print_add, print_remove, key_received)
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 |