Access to sdl2 API underneath kivy?

125 views
Skip to first unread message

Daniel Holth

unread,
Apr 4, 2019, 8:47:07 PM4/4/19
to Kivy users support
Has anyone tried to gain lower level access to the sdl2 API under kivy? I am curious about writing games that use keyboard or joystick input. For these it's more useful to poll getkeyboardstate() or the controller at the simulation frequency instead of keeping track of keydown/keyup events. Also kivy does not currently handle joystick add/remove events.

I've tried compiling a special version of pysdl2-cffi with no linking. If imported after kivy this will find the symbols from kivy's 2.0.5 sdl2 on OSX and Linux, maybe not on windows, and the full joystick API should become available:

https://bitbucket.org/dholth/pysdl2-cffi/src/95a589a421c87fe61180f15ba85be8acbbe8c0d3/sdl/__init__.py?at=default&fileviewer=file-view-default#__init__.py-11099

And getkeyboardstate() which returns an array of all keycodes and whether they are pressed. But I think all events not understood by kivy would be eaten.

In summary I think it would be interesting to be able to use low level sdl2 for non-mouse/touch input in kivy. Has it been tried?

Thanks,

Daniel

Daniel Holth

unread,
Apr 6, 2019, 9:04:11 PM4/6/19
to Kivy users support

After importing kivy.app (which loads SDL2) we use SDL_AddEventListener() to insert joystick and controller event listening into the SDL2 event system without needing to modify kivy.

Whenever a controller is added or removed, we open or close that controller, maintaining a set of all available joysticks at any time.

But I don't know how to make it Kivy-ish yet. Perhaps the available joysticks should be an observable list, or it should be possible to bind to properties of each open joystick.

Daniel Holth

unread,
Apr 9, 2019, 11:59:15 PM4/9/19
to Kivy users support

Here is a demo package that includes a limited SDL2 binding accessible from a Kivy app. "import kivyjoy" is a SDL2 binding and kivyjoy.controller is the beginnings of a Kivy integration.

The SDL keyboard state (poll whether any key is up or down without keeping track of keyup/keydown events yourself), and of course the GameController API, would be useful in a game. It should be possible to make an xbox controller rumble for example. I'm testing it with third party bluetooth Nintendo Switch controllers, and with the JoyCon which unfortunately doesn't show up as a SDL2 GameController by default.

This code hooks the SDL2 event loop and keeps track of all connected controllers in a DictProperty. https://bitbucket.org/dholth/kivyjoy/src/default/kivyjoy/controller.py

If you compile the binding and run demo.py, it should print game controller and joystick events to the console.

Alexander Taylor

unread,
Apr 10, 2019, 4:26:31 PM4/10/19
to Kivy users support
 Thanks for the report about it, I don't have much input on the topic but I'm sure that people will find this useful!
Reply all
Reply to author
Forward
0 new messages