[pygame] not getting joystick events in 2.0

Skip to first unread message

Brian Sturk

Feb 22, 2021, 12:37:51 AM2/22/21
to pygame...@seul.org
I upgraded to 2.0 recently and my joystick code no longer gets events with code that worked w/ 1.x. All
other events are coming through OK (mouse, keyboard, window, etc).

I've spent the evening attempting to debug the issue and have come up empty. Tried 2 different
machines and many different joysticks, all the same behavior. My joystick code does work as far
iterating and printing out the name, etc.

The 2.0 joystick test code on the pygame site does work as I see button down and up messages
coming through.

Is there anything in moving to 2.0 I could be missing?

Here is my joystick init code, load_joysticks is called on startup, after setting up the screen:

def load_joysticks():

global _joystick_present
global _joystick_info


msg = ''

count = pygame.joystick.get_count()

if count == 0:
_joystick_present = False

msg = 'WARNING: No joystick(s) found'

_joystick_info = msg


msg = str( count ) + ' joystick(s)'
_joystick_info = msg
_joystick_present = True

for i in range( count ):

joystick = pygame.joystick.Joystick( i )

name = joystick.get_name().rstrip()
msg = ':' + name + ' - ' + str( joystick.get_numbuttons() ) + ' button(s)'

_joystick_info += msg

My event queue code:

e = None

if _poll:
e = pygame.event.poll() ## grab event now

e = pygame.event.wait() ## wait for a GUI event rather than chewing the CPU

... <snip> ...

elif e.type is JOYBUTTONUP or e.type is JOYHATMOTION or e.type is JOYAXISMOTION:

print 'main(): !!joystick event!!' ## <---- I do not see this print at all when using a joystick w/ 2.0

ret = handle_joystick( e )

I've also tried looping using

for event in pygame.event.get():
e = event

as in the example, still no events.

Any ideas greatly appreciated.


| Brian Sturk ------- http://www.briansturk.com ------ | .> )\,^a__
|-------------------------. _______ Ethical Hacker ___ |( _ _)/ /-."~
| --- C/C++/Python/ASM `----------------------------| `( )_ )/
| Linux/BSD/OSX/Windows embedded/system programming --- |_<_s_<_s

Brian Sturk

Feb 22, 2021, 12:49:32 PM2/22/21
to pygame...@seul.org
Replying to my own issue.  I think I have figured it out.  The joystick
objects going out of scope seems to be the issue.  I didn't have this
issue w/ 1.x but I can see how this is now the right way.  I'm tucking
away the joysticks in an array, and I am now getting events.

Is this the "right way" to do things?  The test program just init's the
joystick for every game loop which seems odd to me.  Is tucking them
away after init'ing correct?


Irv Kalb

Apr 1, 2021, 2:41:44 PM4/1/21
to pygame...@seul.org
I have tracked down a huge slowdown on loading fonts with pygame.font.SysFont

Mac OSX 11.2.3
Python 3.9.1
Pygame 2.0.1

If I load a font like the system font using None with a call to pygame.font.font, it happens almost immediately.

But if I load a font by name using pygame.font.SysFont, it's taking over 7 seconds on my Mac to load. It doesn't seem to matter which font I try to use.

Sample program:

import sys
import pygame
import time

screen = pygame.display.set_mode((400, 400))
clock = pygame.time.Clock()

now = time.time()
font1 = pygame.font.Font(None, 24)
print('Loading System font took', time.time() - now)

now = time.time()
font2 = pygame.font.SysFont('Arial', 24)
print('Loading Arial font took', time.time() - now)

while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:

screen.fill((255, 255, 255))


pygame 2.0.1 (SDL 2.0.14, Python 3.9.1)
Hello from the pygame community. https://www.pygame.org/contribute.html
Loading System font took 0.0006210803985595703
Loading Arial font took 7.058471202850342

(I don't have a setup to test this on Windows.)

This seems like a rather excessive amount of time as it slows down that start up of many of my programs.

It seems to be related to how long it takes to get the list of fonts. If I add this before the call to SysFont:

now = time.time()
fontList = pygame.font.get_fonts()
print('Getting fonts took', time.time() - now)

I get:

Loading System font took 0.0006549358367919922
Getting fonts took 7.013390064239502
Loading Arial font took 0.0004830360412597656

Anyone else seeing this?

Can someone tell me how to send in a bug report for this?



Irv Kalb

Apr 4, 2021, 5:36:46 PM4/4/21
to pygame...@seul.org
[I apologize if this is a duplicate, but I haven't seen this message show up on the list.]

Greg Ewing

Apr 4, 2021, 6:30:11 PM4/4/21
to pygame...@seul.org
On 5/04/21 9:29 am, Irv Kalb wrote:
> If I load a font like the system font using None with a call to pygame.font.font, it happens almost immediately.
> But if I load a font by name using pygame.font.SysFont, it's taking over 7 seconds on my Mac to load. It doesn't seem to matter which font I try to use.

Is it slow every time you run it, or are subsequent runs faster?

I don't have an OSX 11 system handy to try it on, but I tried
it on 10.12.6 and got this:

First run:
Loading System font took 0.015527009963989258
Loading Arial font took 4.879987001419067

Second run:
Loading System font took 0.00061798095703125
Loading Arial font took 0.05048775672912598

Third run:
Loading System font took 0.0006070137023925781
Loading Arial font took 0.0516507625579834

So for me it took about 4.9 seconds the first time, but was
a lot faster once it was warmed up.


Jason Marshall

Apr 4, 2021, 6:52:33 PM4/4/21
to pygame...@seul.org
If I remember correctly, there's a external call to fc-list that is very slow. At one time, I intended to improve this situation by removing the fc-list dependency on Mac and Linux and instead collecting only the needed font data by using freetype and/or Mike C. Fletcher's TTFQuery, but I got too busy.


René Dudfield

Apr 4, 2021, 6:53:00 PM4/4/21
to pygame...@seul.org

There probably should be an open issue for this. The initial query is quite slow unfortunately, and I can’t find a quicker query. What can be done is to use a known set of fonts for different or some subset of OS versions... Apple did/does publish these lists somewhere, so a fix would be to first look at these lists and then only query if the font can not be found.

For many/most fonts that people use with SysFont (like Arial) this would hit the fast path and avoid this slow query.


René Dudfield

Apr 4, 2021, 6:55:12 PM4/4/21
to pygame...@seul.org
The Mac font lists are in this closed issue: https://github.com/pygame/pygame/issues/179
Reply all
Reply to author
0 new messages