Last active
April 28, 2024 05:05
-
-
Save ugovaretto/9e5ed4f002cb4d767f4b347861ae51fa to your computer and use it in GitHub Desktop.
Map PlayStation 4 button to pyglet joystick buttons
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Map pyglet joystick buttons between ps4 and pyglet controller | |
def pyglet_raw_to_ps4_button_dict() -> dict[str, str]: | |
"""Convert raw button names to PS4 button names. | |
Returns: | |
dictionary mapping Pyglet raw button names to PS4 button names | |
The "0x9:" prefix is omitted. | |
In order to map the raw name to the PS4 name | |
take the last character of the passed string, like | |
raw_button_name = ... | |
py2ps4 = pyglet_raw_to_ps4_button_dict() | |
ps4_button_name = py2ps4[raw_button_name[-1]] | |
""" | |
# All raw names start with the prefix "0x9:" | |
# e.g. for the the 'circle' button the name is "0x9:3" | |
pyglet_to_ps4_button = dict() | |
pyglet_to_ps4_button["1"] = "square" | |
pyglet_to_ps4_button["2"] = "cross" | |
pyglet_to_ps4_button["3"] = "circle" | |
pyglet_to_ps4_button["4"] = "triangle" | |
pyglet_to_ps4_button["5"] = "L1" | |
pyglet_to_ps4_button["6"] = "R1" | |
pyglet_to_ps4_button["7"] = "L2" | |
pyglet_to_ps4_button["8"] = "R2" | |
pyglet_to_ps4_button["9"] = "share" | |
pyglet_to_ps4_button["a"] = "options" | |
pyglet_to_ps4_button["b"] = "L3" # left stick button | |
pyglet_to_ps4_button["c"] = "R3" # right stick | |
pyglet_to_ps4_button["d"] = "PS" # Playstation button | |
pyglet_to_ps4_button["e"] = "touchpad" # touchpad button | |
return pyglet_to_ps4_button | |
def ps4_to_pyglet_raw_button_dict() -> dict[str, str]: | |
"""Map PS4 button names to Pyglet raw button names. | |
Returns: | |
dictionary mapping PS4 button names to Pyglet raw names (0x9:*) | |
""" | |
ps4_to_pyglet_button = dict() | |
for v, k in pyglet_raw_to_ps4_button_dict().items(): | |
ps4_to_pyglet_button[k] = "0x9:" + v | |
return ps4_to_pyglet_button | |
def pyglet_to_ps4_button_dict() -> dict[int, str]: | |
"""Map Pyglet button ids to PS4 button names. | |
Returns: | |
dictionary mapping Pyglet button id (int) to PS4 button name | |
""" | |
pyglet_to_ps4_button = dict() | |
pyglet_to_ps4_button[0] = "square" | |
pyglet_to_ps4_button[1] = "cross" | |
pyglet_to_ps4_button[2] = "circle" | |
pyglet_to_ps4_button[3] = "triangle" | |
pyglet_to_ps4_button[4] = "L1" | |
pyglet_to_ps4_button[5] = "R1" | |
pyglet_to_ps4_button[6] = "L2" | |
pyglet_to_ps4_button[7] = "R2" | |
pyglet_to_ps4_button[8] = "share" | |
pyglet_to_ps4_button[9] = "options" | |
pyglet_to_ps4_button[10] = "L3" # left stick button | |
pyglet_to_ps4_button[11] = "R3" # right stick | |
pyglet_to_ps4_button[12] = "PS" # Playstation button | |
pyglet_to_ps4_button[13] = "touchpad" # touchpad button | |
return pyglet_to_ps4_button | |
def ps4_to_pyglet_button_dict() -> dict[str, int]: | |
"""Map PS4 button names to Pyglet integer button ids. | |
Returns: | |
dictionary mapping PS4 button names to Pyglet button ids. | |
""" | |
ps4_to_pyglet_button = dict() | |
for v, k in pyglet_raw_to_ps4_button_dict().items(): | |
ps4_to_pyglet_button[k] = "0x9:" + v | |
return ps4_to_pyglet_button |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment