问题
Th Windows event loop typically looks like:
LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
int wmId, wmEvent;
PAINTSTRUCT ps;
HDC hdc;
switch (message)
{
case WM_COMMAND:
wmId = LOWORD(wParam);
wmEvent = HIWORD(wParam);
// Parse the menu selections:
switch (wmId)
{
case IDM_ABOUT:
DialogBox(hInst, MAKEINTRESOURCE(IDD_ABOUTBOX), hWnd, About);
break;
case IDM_EXIT:
DestroyWindow(hWnd);
break;
default:
return DefWindowProc(hWnd, message, wParam, lParam);
}
break;
case WM_PAINT:
hdc = BeginPaint(hWnd, &ps);
// TODO: Add any drawing code here...
EndPaint(hWnd, &ps);
break;
case WM_DESTROY:
PostQuitMessage(0);
break;
default:
return DefWindowProc(hWnd, message, wParam, lParam);
}
return 0;
}
I want to use XInput to check if a D-Pad button is pressed, and if so, draw something on screen. But which event do I listen for? Should I even listen for an event. Once I have handled the button press, how do I tell the program to draw something?
回答1:
I don't think controllers generate events that you catch in the event loop like that. (I wouldn't absolutely swear to it)
Normally what you do is periodically (every frame) call XInputGetState() which will tell you the state of the controller. (You get an XINPUT_STATE filled in.) If XInputGetState() returns ERROR_SUCCESS then you have a valid controller at that index, if XInputGetState() returns something other than ERROR_SUCCESS then you don't have a working controller at that index. (In games that I've worked on we polled user id's 0 through 3 to get a max of 4 controllers, but if you game or app supports more controllers than that you might want to poll higher numbers. Not sure what the max number is)
You'll need to save off the XINPUT_STATE each frame so you can compare this to your current state. By comparing the saved state to the current state you can tell if the buttons have changed. This will also give you analog values for the joysticks and analog triggers.
来源:https://stackoverflow.com/questions/29422685/which-event-to-listen-for-during-xinput-events