2020-12-28 21:00:04

Hello,
I was curious how this might be done, so I looked up how to use joysticks with Pygame. What I was given was generally helpful, except it doesn't quite explain how to check what buttons or directions are being pressed. Any suggestions? A better way than Pygame?

----------
“Yes, sir. I am attempting to fill a silent moment with non-relevant conversation.”
“You don’t tell me how to behave; you’re not my mother!”
“Could you please continue the petty bickering? I find it most intriguing.” – Data (Star Trek: The Next Generation)

2020-12-28 23:35:10

Just look up SDL2 and joysticks/gamepads in general, that will explain everything you need to know, since pygame is just a wrapper for SDL/SDL2.

2020-12-29 00:49:50

@2 is correct. Look up SDL2, though, not sure if it changed between versions.

2020-12-29 01:53:39

Pyglet has controller support too.

Shameless plug here: Earwax lets you specify button numbers and hat directions when you define actions, along with keyboard and mouse buttons.

-----
I have code on GitHub

2020-12-29 03:34:21

Pygame's controller support doesn't give you rumble support though. I do not really like pyglet all that much.

Facts with Tom MacDonald, Adam Calhoun, and Dax
End racism
End division
Become united

2020-12-29 03:58:28

Yeah, I've never liked Piglet. Optimally, I'd have rumble support as well.

----------
“Yes, sir. I am attempting to fill a silent moment with non-relevant conversation.”
“You don’t tell me how to behave; you’re not my mother!”
“Could you please continue the petty bickering? I find it most intriguing.” – Data (Star Trek: The Next Generation)

2020-12-29 08:41:32 (edited by Alan 2020-12-29 12:22:27)

HEY, just in case you are still interested, I added joystick support for pygame aplications on the past.
Basically, you initialize an instance of pygame.joystick.joystick(id), and check the state of a button by myjoystick.get_button(buttonId). for axes, you get the current value with: myjoystick.get_axis(axisId). In this case, it returns a value from -1.0 to 1.0 representing the current state.
of course, checking input this way is not optimal at all, so soon or later you'll need to write your own snipets. My aproach is a getState function that returns a state object with all current controller values for buttons and axes, so I can compare it with a previous joystick state.
I can share some code if someone is interested, but it's not hard to write your own wrapper.
Finally, there should be a lot of other options out there, but hopefully it could help you, at least for experimenting purposes.
Happy codinn.
Edit: I forgot to mention that you can listen for events such as pygame.JOYBUTTONDOWN or pygame.JOYBUTTONUP, etc. Event.button represents the button that has been pressed or released, I think. I am writing this topic from my phone, cannot test code or consult the documentation, but this is the main concept.
I didn't use this last aproach because writing my own button_pressed funtion seemed easier checking and comparing an object representing the joystick state than dealing with different event types, JOYBUTTONDOWN, JOYAXISMOTION, etc.

2020-12-29 14:24:45

@5
Neither does Pyglet, sadly.

-----
I have code on GitHub

2020-12-29 19:33:04

Well maybe digging into SDL2 can fix that, I don't know. I do know that if I play a game with a controller, and it doesn't have rumble support, I'd just as soon switch back to keyboard unless there's a really good reason knot to.

Facts with Tom MacDonald, Adam Calhoun, and Dax
End racism
End division
Become united

2020-12-29 23:58:30

SDL2's joystick support is wonderful. It needs a set of joystick button mappings to function, though. Such a mapping database is available here. To load it, use SDL_GameControllerAddMapping, SDL_GameControllerAddMappingsFromFile or SDL_GameControllerAddMappingsFromRW to load these. The game controller API is mapped on top of the joystick API. Once loaded, call SDL_NumJoysticks to check for joysticks/game controllers, then use SDL_GameControllerOpen to open one. Then you can use the other functions from either the Joystick and Game Controller APIs. For force feedback, use this API.

"On two occasions I have been asked [by members of Parliament!]: 'Pray, Mr. Babbage, if you put into the machine wrong figures, will the right answers come out ?' I am not able rightly to apprehend the kind of confusion of ideas that could provoke such a question."    — Charles Babbage.
My Github

2020-12-30 00:36:32

@10
Wow, OK, that's pretty cool.

Does that mean force feedback works from Python, it's just not supported natively by either Pyglet or Pygame?

-----
I have code on GitHub

2020-12-30 01:34:26

You could try PySDL2, which is a binding to SDL in python. It should support your needs.

2020-12-30 05:36:25 (edited by Ethin 2020-12-30 05:38:00)

Agreed with 12. Use PySDL2. (Note: I've had trouble using PySDL2 on Windows. In particular, I had to modify the source code to get it to find sdl2.dll... Its search path is kinda weird, though I don't remember it off the top of my head.)
@11
Its unlikely that Pygame/Piglet, if they do use SDL2 underneath, couldn't add force-feedback (or any other SDL2-based API that isn't obsoleted by something Python provides in its stdlib). Its more likely that they just don't want to. Assuming, mind, that both are powered by SDL2.

"On two occasions I have been asked [by members of Parliament!]: 'Pray, Mr. Babbage, if you put into the machine wrong figures, will the right answers come out ?' I am not able rightly to apprehend the kind of confusion of ideas that could provoke such a question."    — Charles Babbage.
My Github

2021-01-01 04:09:04

Would it be correct to assume that the majority of features of PySDL2 are aimed at ECS? See this for a possible explanation for this assumption

2021-01-01 08:01:33

@13
No worries mate, you just pip install pysdl2-dll and all is fixed. Got it working beautifully. Thanks all for your help and suggestions.

-----
I have code on GitHub

2021-01-01 19:37:32

@15, oh, I didn't know that package even existed. Thanks for that tip!

"On two occasions I have been asked [by members of Parliament!]: 'Pray, Mr. Babbage, if you put into the machine wrong figures, will the right answers come out ?' I am not able rightly to apprehend the kind of confusion of ideas that could provoke such a question."    — Charles Babbage.
My Github

2021-01-01 21:10:13

@16
Actually it was @12 who told me about it, so I can't take any credit.

It's good stuff though.

-----
I have code on GitHub