# SDL Joystick!

**URL:** <https://discourse.libsdl.org/t/sdl-joystick/12792>\
**Category:** SDL Development\
**Created:** [November 30, 2005, 11:22pm UTC](https://discourse.libsdl.org/t/sdl-joystick/12792 "2005-11-30T23:22:48Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![Golgoth](https://discourse.libsdl.org/letter_avatar_proxy/v4/letter/g/e8c25b/32.png) [@Golgoth](https://discourse.libsdl.org/u/Golgoth)\
**Post date:** [November 30, 2005, 11:22pm UTC](https://discourse.libsdl.org/t/sdl-joystick/12792/1 "2005-11-30T23:22:48Z")

</div>

Hi all!

We are currently working on implementing joystick input with sdl 1.2.8 on  
windows XP and VS 2003!

We are using a “xbox controller” via “super joy box 10” adaptor!

it all goes well exept for several things…

Strange factor… SDL\_JoystickGetAxis  
we get control over the left trigger using SDL\_JoystickGetAxis(m\_joystick, 4);  
but the right trigger is not responding with SDL\_JoystickGetAxis(m\_joystick,  
5); makes sense since im reporting only 5 axis available.  
How do we get controller over the right trigger?

so far, we get feedbacks from all buttons, left and right axis… no problem  
there but…

How do we get control over the DPAD (cross) up, down, left, right etc…

we figured it must be the SDL\_JoystickGetHat  
what is this for exacly?

we ve tried:

if (SDL\_JoystickGetHat(m\_joystick, SDL\_HAT\_UP))  
Trace(“SDL: Joystick Hat UP pressed.\n”);

and

case SDL\_JOYHATMOTION:  
if (m\_event.jhat.hat & SDL\_HAT\_UP)  
Trace(“SDL: Joystick Hat UP pressed.\n”);

without success…

what did we missed here? anyone can help clear this up?

thx a million!

---

<div class="post-metadata">

**Author:** ![Golgoth](https://discourse.libsdl.org/letter_avatar_proxy/v4/letter/g/e8c25b/32.png) [@Golgoth](https://discourse.libsdl.org/u/Golgoth)\
**Post date:** [December 1, 2005, 12:48am UTC](https://discourse.libsdl.org/t/sdl-joystick/12792/2 "2005-12-01T00:48:49Z")

</div>

Golgoth \<neosettlers [sympatico.ca](http://sympatico.ca)\> writes:

this works:

case SDL\_JOYHATMOTION:  
if (m\_event.jhat.value & SDL\_HAT\_UP)

will be nice to not use sdl events… how to?

---

<div class="post-metadata">

**Author:** ![David\_Olofson](https://discourse.libsdl.org/user_avatar/discourse.libsdl.org/david_olofson/32/115_2.png) [@David\_Olofson](https://discourse.libsdl.org/u/David_Olofson)\
**Post date:** [December 1, 2005, 6:48am UTC](https://discourse.libsdl.org/t/sdl-joystick/12792/3 "2005-12-01T06:48:04Z")

</div>

[…]

> if (SDL\_JoystickGetHat(m\_joystick, SDL\_HAT\_UP))  
> Trace(“SDL: Joystick Hat UP pressed.\n”);  
> […]

According to the documentation, SDL\_JoystickGetHat() _returns_ a Uint8  
hat state. The last argument is an integer telling it which hat to  
read. (There can be several hats on one joystick. Mine has 4 IIRC;  
two on the actual stick and two on the throttle handle…)

So, the code should read something like:

```
if (SDL_JoystickGetHat(m_joystick, hat) & SDL_HAT_UP)
	Trace("SDL: Joystick Hat UP pressed.\n");

```

where “hat” is the index of the hat you want to read.

//David Olofson - Programmer, Composer, Open Source Advocate

.- Audiality -----------------------------------------------.  
| Free/Open Source audio engine for games and multimedia. |  
| MIDI, modular synthesis, real time effects, scripting,… |  
`-----------------------------------\> [http://audiality.org](http://audiality.org) -’  
— [http://olofson.net](http://olofson.net) — [http://www.reologica.se](http://www.reologica.se) —On Thursday 01 December 2005 00.22, Golgoth wrote:

---

<div class="post-metadata">

**Author:** ![icculus](https://discourse.libsdl.org/user_avatar/discourse.libsdl.org/icculus/32/81_2.png) [@icculus](https://discourse.libsdl.org/u/icculus)\
**Post date:** [December 1, 2005, 12:19pm UTC](https://discourse.libsdl.org/t/sdl-joystick/12792/4 "2005-12-01T12:19:28Z")

</div>

> case SDL\_JOYHATMOTION:  
> if (m\_event.jhat.value & SDL\_HAT\_UP)
> 
> will be nice to not use sdl events… how to?

After you initialize everything:

SDL\_JoystickEventState(SDL\_QUERY); /\* we will manually check \*/

Then when you want to check joystick state:

SDL\_JoystickUpdate(); /\* call this once per frame. _/  
value = SDL\_JoystickGetAxis(myJoystick, 0); /_ get axis 0’s state \*/

This way you don’t have to use the event loop for joysticks. Make sure  
you call SDL\_JoystickUpdate() once a frame, and then query for the  
specific things you care about.

–ryan.

---

<div class="post-metadata">

**Author:** ![James\_O\_Meara](https://discourse.libsdl.org/letter_avatar_proxy/v4/letter/j/b9bd4f/32.png) [@James\_O\_Meara](https://discourse.libsdl.org/u/James_O_Meara)\
**Post date:** [December 4, 2005, 6:21am UTC](https://discourse.libsdl.org/t/sdl-joystick/12792/5 "2005-12-04T06:21:52Z")

</div>

Hey, I might be able to help a little bit. I’m making an actual Xbox game, so  
I’m dealing with similiar stuff to you. Don’t know if this’ll help much, but  
here’s everything I can offer:

I’ve set up a struct for my controller, like so:

//gamepad struct  
struct INPUT{  
SDL\_Joystick \*GAMEPAD; //pointer for the gamepad  
Sint16 abutton,bbutton,xbutton,ybutton,whitebutton,blackbutton; //signed ints  
for buttons  
Sint16 rtrigger,ltrigger,startbutton,backbutton; //signed ints for buttons  
Sint16 rstick,lstick; //signed ints for joystick clicks  
Sint16 joyx, joyy; //signed ints for left joystick axes  
Sint16 rjoyx, rjoyy; //signed ints for right joystick axes  
Sint16 dpad; //signed int for the dpad  
const static int threshold = 3200; //controller deadzone  
};

INPUT pad; //our controller

I’ve then made a function that updates the input:

//this updates the joypad states, so we can check for input  
void get\_input(void)  
{  
//update the joystick  
SDL\_JoystickUpdate(); //refreshes the gamepad state

```
//DPAD
pad.dpad = SDL_JoystickGetHat(pad.GAMEPAD, 0);	//Get DPAD

//AXES
pad.joyx = SDL_JoystickGetAxis(pad.GAMEPAD, 0);	//Get Left Joystick X-value
pad.joyy = SDL_JoystickGetAxis(pad.GAMEPAD, 1);	//Get Left Joystick Y-value
pad.rjoyx = SDL_JoystickGetAxis(pad.GAMEPAD,2);	//Get Right Joystick X-value
pad.rjoyy = SDL_JoystickGetAxis(pad.GAMEPAD,3);	//Get Right Joystick Y-value

//BUTTONS
pad.abutton = SDL_JoystickGetButton(pad.GAMEPAD, 0);	//Get A-Button (0)
pad.bbutton = SDL_JoystickGetButton(pad.GAMEPAD, 1);	//Get B-Button (1)
pad.xbutton = SDL_JoystickGetButton(pad.GAMEPAD,2);	//Get X-Button (2)
pad.ybutton = SDL_JoystickGetButton(pad.GAMEPAD,3);	//Get Y-Button (3)
pad.blackbutton = SDL_JoystickGetButton(pad.GAMEPAD, 4);//Get Black Button (4)
pad.whitebutton = SDL_JoystickGetButton(pad.GAMEPAD, 5);//Get White Button (5)
pad.ltrigger = SDL_JoystickGetButton(pad.GAMEPAD,6);	//Get Left Trigger (6)
pad.rtrigger = SDL_JoystickGetButton(pad.GAMEPAD,7);	//Get Right Trigger (7)
pad.startbutton = SDL_JoystickGetButton(pad.GAMEPAD,8);	//Get Start Button (8)
pad.backbutton = SDL_JoystickGetButton(pad.GAMEPAD, 9);	//Get Back Button (9)
pad.lstick = SDL_JoystickGetButton(pad.GAMEPAD,10);	//Get Left Stick Click	(10)
pad.rstick = SDL_JoystickGetButton(pad.GAMEPAD,11);	//Get Right Stick Click	(11)

```

}

> From here you can use a function to check the values on all those variables.

Also, some of this will be a little bit different because with SDLx the L and R  
triggers are converted to buttons. Hopefully it’s enough to help you with  
everything, though.

---

<div class="post-metadata">

**Author:** ![Golgoth](https://discourse.libsdl.org/letter_avatar_proxy/v4/letter/g/e8c25b/32.png) [@Golgoth](https://discourse.libsdl.org/u/Golgoth)\
**Post date:** [December 10, 2005, 4:05am UTC](https://discourse.libsdl.org/t/sdl-joystick/12792/6 "2005-12-10T04:05:32Z")

</div>

Thx for the help guys!

Theres still problems remaining!

my left and right axis click are triggered by button 6 and 7…

index button 10 and 11 does not respond to any game pad buttons…

the left trigger work with:  
SDL\_JoystickGetAxis(m\_joystick, 4);

and the right trigger is not doing any thing so far!

looks hocus pocus to me!

any ideas?

thx again!

---

<div class="post-metadata">

**Author:** ![Jacob\_Kolding](https://discourse.libsdl.org/letter_avatar_proxy/v4/letter/j/ed655f/32.png) [@Jacob\_Kolding](https://discourse.libsdl.org/u/Jacob_Kolding)\
**Post date:** [April 12, 2006, 1:11pm UTC](https://discourse.libsdl.org/t/sdl-joystick/12792/7 "2006-04-12T13:11:55Z")

</div>

Golgoth \<neosettlers sympatico.ca\> writes:

> Thx for the help guys!
> 
> Theres still problems remaining!
> 
> my left and right axis click are triggered by button 6 and 7…
> 
> index button 10 and 11 does not respond to any game pad buttons…
> 
> the left trigger work with:  
> SDL\_JoystickGetAxis(m\_joystick, 4);
> 
> and the right trigger is not doing any thing so far!
> 
> looks hocus pocus to me!
> 
> any ideas?
> 
> thx again!

I’m having the exact same problem  
only with a Joy Box 13 for GameCube controllers.  
Did you find a solution?

/Jacob Kolding
