Last active
May 19, 2024 01:45
-
-
Save TheNathannator/466cc79ae6535e4dfab23fe44708380f to your computer and use it in GitHub Desktop.
Using GameInput to read raw Xbox One controller data
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <cassert> | |
#include <iostream> | |
#include <iomanip> | |
#include <memory> | |
#include <windows.h> | |
#include <wrl.h> | |
// Install the https://www.nuget.org/packages/Microsoft.GameInput package for this example | |
#include <GameInput.h> | |
namespace WRL = Microsoft::WRL; | |
int main() | |
{ | |
std::cout << "Hello World!\n"; | |
// Initialize GameInput | |
WRL::ComPtr<IGameInput> gameInput; | |
assert(SUCCEEDED(GameInputCreate(&gameInput))); | |
uint64_t lastTimestamp = 0; | |
while (Sleep(1), true) | |
{ | |
// Poll for raw reports | |
WRL::ComPtr<IGameInputReading> reading; | |
if (SUCCEEDED(gameInput->GetCurrentReading(GameInputKindRawDeviceReport, nullptr, &reading))) | |
{ | |
// Ignore unchanged reports | |
uint64_t timestamp = reading->GetTimestamp(); | |
if (lastTimestamp == timestamp) | |
continue; | |
lastTimestamp = timestamp; | |
// Read report | |
WRL::ComPtr<IGameInputRawDeviceReport> rawReport; | |
assert(reading->GetRawReport(&rawReport)); | |
auto info = rawReport->GetReportInfo(); | |
size_t size = rawReport->GetRawDataSize(); | |
std::cout << "Report ID: " << info->id << ", " << "size: " << size; | |
// Read report data | |
byte* buffer = new byte[size]; | |
assert(rawReport->GetRawData(size, buffer) == size); | |
std::cout << ", "; | |
for (int i = 0; i < size; i++) | |
std::cout << std::hex << std::setw(2) << std::setfill('0') << (int)buffer[i]; | |
delete[] buffer; | |
std::cout << std::endl; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment