// Copyright (c) Asobo Studio, All rights reserved. www.asobostudio.com //------------------------------------------------------------------------------ // // SimConnect Joystick Control Sample // // Description: // Use the "z" key to step through the events sent by the Joystick // including X,Y,Z axes, Slider and Hat switch //------------------------------------------------------------------------------ #include #include #include #include #include "SimConnect.h" int quit = 0; HANDLE hSimConnect = NULL; int current = 0; int iDeviceID = 0; static enum GROUP_ID { GROUP_0 }; #define NB_BUTTONS 10 static enum INPUT_ID { INPUT_BUTTON }; static enum EVENT_ID { EVENT_BUTTON }; char szName[32]; void CALLBACK MyDispatchProcJ(SIMCONNECT_RECV* pData, DWORD cbData, void *pContext) { HRESULT hr; switch(pData->dwID) { case SIMCONNECT_RECV_ID_CONTROLLERS_LIST: { SIMCONNECT_RECV_CONTROLLERS_LIST* pList = (SIMCONNECT_RECV_CONTROLLERS_LIST*)pData; for (int i=0; idwSize; ++i) { if (_stricmp(pList->rgData[i].DeviceName, "Controller") == 0) { iDeviceID = pList->rgData[i].DeviceId; break; } } for (int i = 0; i < NB_BUTTONS; ++i) { hr = SimConnect_MapClientEventToSimEvent(hSimConnect, EVENT_BUTTON + i); hr = SimConnect_AddClientEventToNotificationGroup(hSimConnect, GROUP_0, EVENT_BUTTON + i); snprintf(szName, sizeof(szName), "joystick:%d:Button:%d", iDeviceID, i); hr = SimConnect_MapInputEventToClientEvent_EX1(hSimConnect, INPUT_BUTTON + i, szName, EVENT_BUTTON + i); } hr = SimConnect_SetNotificationGroupPriority(hSimConnect, GROUP_0, SIMCONNECT_GROUP_PRIORITY_HIGHEST); } break; case SIMCONNECT_RECV_ID_EVENT: { SIMCONNECT_RECV_EVENT *evt = (SIMCONNECT_RECV_EVENT*)pData; if (evt->uEventID >= EVENT_BUTTON && evt->uEventID <= EVENT_BUTTON + NB_BUTTONS) { printf("\nButton%d value:%d", evt->uEventID, evt->dwData); break; } break; } case SIMCONNECT_RECV_ID_QUIT: { quit = 1; break; } default: break; } } void testInput() { HRESULT hr; if (SUCCEEDED(SimConnect_Open(&hSimConnect, "Joystick Input", NULL, 0, 0, 0))) { printf("\nConnected to Flight Simulator!"); SimConnect_EnumerateControllers(hSimConnect); while( 0 == quit ) { SimConnect_CallDispatch(hSimConnect, MyDispatchProcJ, NULL); Sleep(1); } hr = SimConnect_Close(hSimConnect); } } int __cdecl _tmain(int argc, _TCHAR* argv[]) { testInput(); return 0; }