Reading all the buttons from a joystick

Monkey Forums/Monkey Code/Reading all the buttons from a joystick

mr_twister(Posted 2014) [#1]
I don't know if this has been posted before but it's been quite the useful discovery for me.

Monkey actually reads (a max of) 32 buttons per joystick but only maps a handful of them to the constants we all know (JOY_A, JOY_B, JOY_X, JOY_MENU, JOY_BACK, etc)...

The rest of the buttons are still part of the internal array of states that monkey allocates and keeps track of, so if you can figure out the "key code" assigned to them you can actually read their state.
Luckily, that's awfully simple since joystick entries are consecutive groups (1 group per joystick) of 32 entries (1 entry per button) starting at KEY_JOY0, which means that you can query their "hit" and "down" with nothing more than this:

Function RawJoyButtonDown: Int(joyNum: Int, bttnNum: Int)
	If joyNum < 0 Or joyNum > 3 Or bttnNum < 0 Or bttnNum > 31 Then Return 0
	Return KeyDown(KEY_JOY0 + joyNum*32 + bttnNum)
End

Function RawJoyButtonHit: Int(joyNum: Int, bttnNum: Int)
	If joyNum < 0 Or joyNum > 3 Or bttnNum < 0 Or bttnNum > 31 Then Return 0
	Return KeyHit(KEY_JOY0 + joyNum*32 + bttnNum)
End

I'm currently using this to improve the gamepad support in my game and it's been working perfectly so far. I truly hope someone else can benefit from this code too.


Nobuyuki(Posted 2014) [#2]
I do this with system charcodes to get foreign keyboard keys in my bind system. Works pretty well.


Sensei(Posted 2014) [#3]
Hi there. Where would one place this and use the code example above?


mr_twister(Posted 2014) [#4]
Just add those two functions to your code (outside of any class declaration if you want them to be global).
You can then use them instead of (or together with) JoyHit() or JoyDown() to read joystick buttons.


Sensei(Posted 2014) [#5]
Thanks mr_twister. I did as you suggested and added the following snippet to the joytest.monkey and I found it displaying the same results as the regular JoyDown() commands.

For Local button := 0 Until 32
    DrawText "Button:       " +RawJoyButtonDown(port, button), 200, 10 + (button * 10)
Next


Unfortunately I only have a xbox360 controller attached so can't test more buttons than are usable in Monkey :)


mr_twister(Posted 2014) [#6]
Well yes! it's doing the same as the regular KeyDown/KeyHit but it gives you access to ALL the buttons that Monkey keeps track of, (32) instead of the first few that are available on the XBOX 360 controller and exposed through JOY_A, JOY_B, etc!
I don't have an xbox controller but PERHAPS with this code you can read the "analog stick" buttons"? (pressing the analog sticks down, also known as L3/R3 is PS2/3 gamepads)


Sensei(Posted 2014) [#7]
Thanks for the update mate. The standard hits are already able to get to all buttons, pads and analog sticks on both 360 and ps3 controllers (I have both).
I suppose your code will be useful when using more advanced controllers like flight sticks or steering wheels etc.


mr_twister(Posted 2014) [#8]
Oh you are right, the stick buttons are JOY_LSB and JOY_RSB, respectively.

This code is not only for massive/advanced controllers though, It's more for non-PS3/360 controllers! I have a Genius "PS3-compatible" joystick, and a few buttons (like R3) are not recognized by Monkey as any of the internal constants but can be read with my code. Although I guess that most gamers use either a PS3 or 360 controller these days...