I’m a new sdl user, I’m using it as a replacement for xinput (windows) to support non-xbox controllers on a background autohotkey2 automation script.
I’m using sdl through interop features of ahk, I translate some gamepad chords into keyboards actions to control the shell with a controller. One of the main task I require is switching between programs in the shell (Alt-tabbing), but every time the keyboard action is executed, I receive spurious “button up” events for all buttons pressed at that moment. Other actions which do not affect the window focus work fine.
I’m aware of the many threads around the internet discussing background gamepad/joystick handling, but in my case I receive button events even without the SDL_HINT_JOYSTICK_ALLOW_BACKGROUND_EVENTS
hint, regardless of the window with focus.
The script is ran by an interpreter which has no visible window (just a tray icon), so I’m also using SDL_HINT_JOYSTICK_THREAD
; this hint makes no difference either.
I’m calling all SDL_SetHint()
before initialization for good measure, and I’m checking actual hint values with SDL_GetHint()
.
Every action which causes a focus change (switching window, interacting with the shell) has the same effect, the issue is not with “Alt tab” specifically. I’m doing my test with a “XBox series” bluetooth controller.
I’m fairly experienced in C programming (both Windows and Linux), but not with SDL. The documentation “wiki” is very sparse.
I’m trying to keep this question out of the autohotkey realm, because it doesn’t do much other than calling native SDL functions, it’s a pretty basic event polling loop. I’ll post just a small snippet for now:
controllers := Map()
SDL2 := SDL2DLL()
SDL2.SDL_SetHint("SDL_JOYSTICK_THREAD", "1")
SDL2.SDL_SetHint("SDL_JOYSTICK_ALLOW_BACKGROUND_EVENTS", "1")
;SDL2.SDL_SetHint("SDL_JOYSTICK_RAWINPUT", "1")
SDL2.SDL_Init(SDL_INIT_GAMECONTROLLER := 0x00002000)
joystickTest()
SDL2.SDL_Quit()
return
joystickTest() {
SDL2.SDL_JoystickEventState(true)
loop SDL2.SDL_NumJoysticks() {
openController(A_index - 1)
}
;AHK doesn't support union, need Buffer to receive SDL_Event
evtBuffer := Buffer(56, 0)
homeButton := false
taskSwitching := false
OutputDebug("JOYSTICK_ALLOW_BACKGROUND_EVENTS=" SDL2.SDL_GetHint("SDL_JOYSTICK_ALLOW_BACKGROUND_EVENTS"))
OutputDebug("JOYSTICK_THREAD=" SDL2.SDL_GetHint("SDL_JOYSTICK_THREAD"))
loop {
PollEvent := SDL2.SDL_PollEvent(evtBuffer)
evt := SDL2DLL.SDL_Event(evtBuffer)
if (PollEvent = 0) {
continue
}
switch (evt.type) {
case SDL_EventType.SDL_CONTROLLERBUTTONDOWN, SDL_EventType.SDL_CONTROLLERBUTTONUP:
ToolTip(SDL2.SDL_JoystickNameForIndex(controllers[evt.cbutton.which].index) "`nButton: " evt.cbutton.button "`nPressState: " evt.cbutton.state "`nEvent: " evt.type)
switch (evt.cbutton.button) {
case SDL_EventType.SDL_CONTROLLER_BUTTON_BACK:
backButton := (evt.type = SDL_EventType.SDL_CONTROLLERBUTTONDOWN)
OutputDebug("back " (evt.type = SDL_EventType.SDL_CONTROLLERBUTTONDOWN ? "down" : "up"))
I’m using SDL2 vers. 2.30.1, on Windows. Can anyone reproduce? Is this expected?