Skip to content

Instantly share code, notes, and snippets.

@minkcv
Last active June 4, 2024 23:29
Show Gist options
  • Save minkcv/ccb04c62c287f6bf7c01fcb3d2014474 to your computer and use it in GitHub Desktop.
Save minkcv/ccb04c62c287f6bf7c01fcb3d2014474 to your computer and use it in GitHub Desktop.
Buttons on devkitPro switch port of SDL2
// This is what devkitPro has done with the SDL keybinds for the switch.
// These are not any of the built in SDL_CONTROLLER_* values or SDL_JOYSTICK_* values from the SDL 2.0 documentation.
// Which makes development very confusing if you haven't read the thread on gbatemp about these.
// The analog sticks are mapped here as buttons. Meaning the analog sticks only report 4 directions each.
// The Analog stick "click in" is reported here so that is nice.
// Snippet from devkitPro/SDL
// src/joystick/switch/SDL_sysjoystick.c
// with added comments indicating numbers for keys
static const HidControllerKeys pad_mapping[] = {
KEY_A, KEY_B, KEY_X, KEY_Y, // 0, 1, 2, 3
KEY_LSTICK, KEY_RSTICK, // 4, 5
KEY_L, KEY_R, // 6, 7
KEY_ZL, KEY_ZR, // 8, 9
KEY_PLUS, KEY_MINUS, // 10, 11
KEY_DLEFT, KEY_DUP, KEY_DRIGHT, KEY_DDOWN, // 12, 13, 14, 15
KEY_LSTICK_LEFT, KEY_LSTICK_UP, KEY_LSTICK_RIGHT, KEY_LSTICK_DOWN, // 16, 17, 18, 19
KEY_RSTICK_LEFT, KEY_RSTICK_UP, KEY_RSTICK_RIGHT, KEY_RSTICK_DOWN, // 20, 21, 22, 23
KEY_SL_LEFT, KEY_SR_LEFT, KEY_SL_RIGHT, KEY_SR_RIGHT // 24, 25, 26, 28
};
// You can see in the cavestory port for the switch that they have just hardcoded these numbers
// Which is how I found out what was going on. Thanks carstene1ns.
// You might want to update your code to use the left joystick to move because the lower left dpad is cramped.
// https://github.com/carstene1ns/nxengine-evo/blob/switch-port/src/input.cpp
// See the __SWITCH__ section for the hardcoded numbers.
// And here is the commit that I believe adds the little shoulder buttons on the side of the joycon that are hidden when the joycons are railed.
// https://github.com/devkitPro/SDL/commit/a1d5cbd8cb3897d2c7f29addde9ec2cd1fc7cb37#diff-1e2cd50726109fb8d6a302b2cbfdfe0b
// FINALLY here is my example code for checking buttons on the switch:
// See the snippet above that has comments added by me with numbers to show which
// numbers are what switch buttons
if (event.type == SDL_JOYBUTTONDOWN)
{
if (event.jbutton.button == 0) // A
{
}
if (event.jbutton.button == 17) // L Stick Up
{
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment