LPDIRECTINPUT8 lpDI; LPDIRECTINPUTDEVICE8 lpKeyboard; // initialize DirectInput HRESULT hr = DirectInput8Create(GetModuleHandle(NULL), DIRECTINPUT_VERSION, IID_IDirectInput8, (void**)&lpDI, NULL); // initialize keyboard device hr = lpDI->CreateDevice(GUID_SysKeyboard, &lpKeyboard, NULL); hr = lpKeyboard->SetDataFormat(&c_dfDIKeyboard); hr = lpKeyboard->SetCooperativeLevel(hwnd, DISCL_FOREGROUND | DISCL_NONEXCLUSIVE); hr = lpKeyboard->Acquire();
DIJOYSTATE2 js; LPDIRECTINPUTDEVICE8 lpJoystick; // create joystick device hr = lpDI->CreateDevice(GUID_SysJoystick, &lpJoystick, NULL); hr = lpJoystick->SetDataFormat(&c_dfDIJoystick2); hr = lpJoystick->SetCooperativeLevel(hwnd, DISCL_FOREGROUND | DISCL_EXCLUSIVE); hr = lpJoystick->Acquire(); // read joystick input hr = lpJoystick->GetDeviceState(sizeof(DIJOYSTATE2), &js); if (FAILED(hr)) { // re-acquire device if lost lpJoystick->Acquire(); } else { // use joystick input int x = js.lX; int y = js.lY; int z = js.lZ; }This code creates a joystick device and sets the data format to the standard joystick format. It sets the cooperative level to allow for foreground access and exclusive access. Finally, it reads the joystick input and uses it to set variables for x, y, and z axes. The package library for LPDIRECTINPUTDEVICE8 is the DirectX Library, which is included in the Windows Software Development Kit (SDK).