# Direct Input: Detecting New Devices

## Recommended Posts

Hi Everyone, I'm using Direct Input for my game controllers and I was wondering if anyone had any advice on how to detect new controllers which are plugged in after they've been enumerated. I've tried just enumerating again but this is quite slow and not something I could call every few seconds for detection. I was wondering if there is some callback for new devices so I don't have to keep enumerating and checking for new ones myself. Any advice would be great! Many Thanks David

##### Share on other sites
mososky    100
Pull it only when the user is in a menu screen. Seems reasonable as the user would probably pause anyway to plug in a device.

##### Share on other sites

That's not gonna do it for me. The menus in my game are quite animated and enumerating causes a noticable jump. I'd need to check every few seconds and thats not gonna look very good. There must be a simple way to check if there has been a new device added or removed without having to enumerate. Then If there is a new/lost device I don't mind a jump while I enumerate to find out what that device is.

Thanks Again
David

##### Share on other sites
Evil Steve    2017
What about running a second thread to constantly enumerate devices (Or maybe wait for a few seconds before each enumeration to avoid thrashing the CPU)?

##### Share on other sites
Nik02    4348
Have you tried to handle WM_DEVICECHANGE?

##### Share on other sites
Quote:
 Original post by Nik02Have you tried to handle WM_DEVICECHANGE?

Thanks Niki02! That seems to be doing the job quite nicely!

Quote:
 Original post by Evil SteveWhat about running a second thread to constantly enumerate devices (Or maybe wait for a few seconds before each enumeration to avoid thrashing the CPU)?

Also thanks Evil Steve, That's A Good Idea. I'll Give That A Go If WM_DEVICECHANGE Fails Me! (you always have a good answer!).

Many Thanks To You Both!
David

##### Share on other sites
howie_007    285
This is what I do since I poll each game loop cycle.

/*************************************************************************    desc:  Poll the device**    ret: HRESULT - result of polling************************************************************************/HRESULT CBaseController::Poll(){	HRESULT hr;	// Poll the device to read the current state    if( FAILED( hr = pDevice->Poll() ) )    {        // DInput is telling us that the input stream has been        // interrupted. We aren't tracking any state between polls, so        // we don't have any special reset that needs to be done. We        // just re-acquire and try again.        hr = pDevice->Acquire();        while( hr == DIERR_INPUTLOST )		{            hr = pDevice->Acquire();		}		// If we encounter a fatal error, return failure.        if( (hr == DIERR_INVALIDPARAM) || (hr == DIERR_NOTINITIALIZED) )		{            return E_FAIL;        }        // If another application has control of this device, return success.        // We'll just have to wait our turn to use the joystick.        if( hr == DIERR_OTHERAPPHASPRIO )		{            return E_FAIL;        }        // hr may be DIERR_OTHERAPPHASPRIO or other errors.  This        // may occur when the app is minimized or in the process of         // switching, so just try again later         return E_FAIL;    }	return DI_OK;}	// Poll