Allegro.cc - Online Community

Allegro.cc Forums » Allegro Development » Question about joystick events

This thread is locked; no one can reply to it. rss feed Print
Question about joystick events
Michael Weiss
Member #223
April 2000

How do I get the joystick number from the joystick event?

I can use: joystick.id (ALLEGRO_JOYSTICK *)
and get an ALLEGRO_JOYSTICK struct...

From that I can figure a lot of things about that particular joystick,
except that I can't seem to figure out how to tell if its joystick 0 or 1
on a system with 2 joysticks installed.

Am I missing something obvious??

Thanks..

NiteHackr
Member #2,229
April 2002

I would look at the function al_get_joystick() to get the number. See also al_get_joystick_active() and al_get_joystick_name()

--
Deluxe Pacman 1 & 2 (free) with source code available
https://nitehackr.github.io/games_index.html

Michael Weiss
Member #223
April 2000

Thanks Neil,

I looked at your answer, but it didn't help.

ALLEGRO_JOYSTICK * al_get_joystick(int num)
only returns the ALLEGRO_JOYSTICK handle struct, you have to pass it num;
I get the same joystick handle struct, from the events it generates.

OK, so I've got the joystick handle struct, what can i do with that?

bool al_get_joystick_active(ALLEGRO_JOYSTICK *joy)
only tells me if a particular joystick is active or not,
not very helpful as I can't imagine that an inactive joystick will be generating events

const char *al_get_joystick_name(ALLEGRO_JOYSTICK *joy)
The text name for both of my joysticks is the same.

Now I don't want to be one of those people who say:
"This is how it was done in allegro 4, why can't I do this anymore?"...but...

This is how it was done in allegro 4: extern JOYSTICK_INFO joy[n];

It was very easy to figure out which joystick was which.

All I want to do is:
When I get a joystick event, to be able to tell what joystick it came from.

Someone must have a multi joystick example, but I have searched and can't find anything.

Thanks

Edgar Reynaldo
Member #8,592
May 2007
avatar

Untested, but this should do what you want :

int getJoystickNum(ALLEGRO_JOYSTICK* joy) {
   for (unsigned int i = 0 ; i < al_get_num_joysticks() ; ++i) {
      if (joy == al_get_joystick(i)) {return i;}
   }
   return -1;
}

Michael Weiss
Member #223
April 2000

That works perfectly!! Thank you so much for your help!!

Peter Hull
Member #1,136
March 2001

I don't disagree with Edgar but take a look at the docs for al_reconfigure_joysticks - if you want to support plugging/unplugging sticks you may need to do a bit of extra work, in case Allegro renumbers them. I don't have >1 joystick to test (and I suspect it's platform dependent) so I don't know if this will be a problem in practice.
I would enumerate the sticks once and put the pointers in my own array, rather than going via al_get_num_joysticks and al_get_joystick every time. Then, handle the ALLEGRO_EVENT_JOYSTICK_CONFIGURATION event as necessary.

Go to: