Hey Malice, I managed to do it with Windows functions in KarBOOM before I switched to Unity:

Originally Posted By: JibbSmart
Basically, using the right windows function, you can poll all connected joysticks at any time, regardless of when they were connected.

OK, so, my code is a mess, and it's hard to be sure, but this will hopefully be helpful:

Code:
#include <windows.h>

UINT __stdcall joyGetPosEx(UINT, long);
#define PRAGMA_API joyGetPosEx;winmm!joyGetPosEx

#define JOYSTICK_SIZE 52
#define JOYSTICK_INDICES 13
#define JOYSTICK_XPOS 2
#define JOYSTICK_YPOS 3
#define JOYSTICK_ZPOS 4
#define JOYSTICK_RPOS 5
#define JOYSTICK_UPOS 6
#define JOYSTICK_VPOS 7
#define JOYSTICK_BUTTONS 8
#define JOYSTICK_POV 10
#define HALF_JOY 32768

DWORD joystick[JOYSTICK_INDICES];

function getJoy(int i) {
	// get joystick info and put it in our global array
	joystick[0] = JOYSTICK_SIZE;
	joystick[1] = JOY_RETURNALL;
	var r = joyGetPosEx(i, (void*)joystick);
	if (hasZAxis[i] == 0) {
		joystick[JOYSTICK_ZPOS] = HALF_JOY; // cancel out z axis when not available
	}
	return r;
}

function joyButton(int i) {
	i--;
	if (joystick[JOYSTICK_BUTTONS] & (1<<i))
		return 1;
	else
		return 0;
}

#define MIN_JOY 0.1
#define MAX_JOY 0.9

function joyNormalize(var value) {
	// here I can define a min and max value -- min gets scaled to zero, and max gets scaled to 1
	value = (value - HALF_JOY) / HALF_JOY;
	value -= sign(value) * minv(abs(value), MIN_JOY); // scale down zero
	value /= MAX_JOY - MIN_JOY; // scale up
	return clamp(value, -1, 1);
}


The important thing is joyGetPosEx (helpful reading material on its parameters here), a windows function (hence the need for #include <windows.h>).
So, if I recall correctly, it goes something like this...

Calling getJoy(n) will poll the nth joystick, populating the global array "joystick" (that DWORD array) with all the details of that joystick, if it is connected.

So, joystick[JOYSTICK_XPOS] will get you the raw x-axis position of the left stick. joyNormalize(joystick[JOYSTICK_XPOS]) will give it to you from -1 to +1, with a deadzone and a clamp on the top defined by MIN_JOY and MAX_JOY, respectively.

The buttons are all flags - individual bits - set in element 8 in the array (JOYSTICK_BUTTONS), so I have a function that lets you check if button i on the last-polled joystick (with getJoy) is being pressed: joyButton(i).

KarBOOM's kars will start each frame by calling "getJoy" and whatever number their joystick is. Then joyButton(i) and any use of joystick[] gives them information about their joystick. If getJoy gets called with a different joystick number before they're done with it for this frame, they'll start getting the wrong values. So you COULD change it to populate a different array for each joystick, which might simplify things.

Hopefully nothing's missing, and I hope that's helpful! I haven't looked at this stuff in a really long time. I understand I've been a bit vague - it's been a big day and I'm tired tongue But feel free to ask if anything isn't clear laugh


Superku apparently had problems with it, though, with some buttons not being responsive.


Formerly known as JulzMighty.
I made KarBOOM!