GenericInput is a HID controller input library written in C++ that aims to clone proprietary controller libraries.
Initialize GenericInput by passing a Window Handle and a boolean to tell GenericInput whether or not to close your application upon failure.
BOOLInitInstance(HINSTANCE hInstance, int nCmdShow)
{
hInst = hInstance; // Store instance handle in our global variableHWND hWnd = CreateWindowW(szWindowClass, szTitle, WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT, 0, CW_USEDEFAULT, 0, nullptr, nullptr, hInstance, nullptr);
if (!hWnd)
{
returnFALSE;
}
if (GenericInputInit(hWnd, FALSE) == ERROR_GEN_FAILURE)
{
returnFALSE;
}
ShowWindow(hWnd, nCmdShow);
UpdateWindow(hWnd);
returnTRUE;
}Add GenericInputDeviceChange to your Windows procedure:
LRESULTCALLBACKWndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
GenericInputDeviceChange(hWnd, message, wParam, lParam);
return0;
}GenericInput will send the registered window a message, if it detects a controller connection event:
LRESULTCALLBACKWndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
switch (message)
{
caseWM_CONTROLLER_CONNECTED:
{
// A controller was connected and wParam is the index of the controller that was connected.
}
caseWM_CONTROLLER_DISCONNECTED:
{
// A controller was disconnected and wParam is the index of the controller that was disconnected.
}
default:
returnDefWindowProc(hWnd, message, wParam, lParam);
}
return0;
}GENERIC_INPUT_STATE is intercompatible with XINPUT_STATE
Call GenericInputGetState with a controller index to get the state of that controller.
voidInputManager::GamepadUpdate(DWORD dwUserIndex)
{
GENERIC_INPUT_STATE gamepadState = { 0 };
if (GenericInputGetState(&gamepadState, dwUserIndex) == ERROR_SUCCESS)
{
if (state.Gamepad.wButtons & CONTROLLER_BUTTON_A)
{
// Do something
}
}
}To get the layout of the current controller, call GenericInputGetLayout with the an index of the desired controller.
voidInputManager::GamepadUpdate(DWORD dwUserIndex)
{
GENERIC_INPUT_STATE gamepadState = { 0 };
if (GenericInputGetState(&gamepadState, dwUserIndex) == ERROR_SUCCESS)
{
controllerType = GenericInputGetLayout(dwUserIndex);
switch (controllerType)
{
caseNC:// Not connected
{
// Change the gui button promptsbreak;
}
case XInput:// Xbox Controller
{
// Change the gui button promptsbreak;
}
caseDS: // PlayStation Controller
{
// Change the gui button promptsbreak;
}
caseNT: // Nintendo Controller
{
// Change the gui button promptsbreak;
}
// Found in the game controller database at compile time. caseSDL: // Generic Controller
{
// Change the gui button promptsbreak;
}
}
}