Jump to content
  • Advertisement
Sign in to follow this  

Receiving Raw Input problem

This topic is 2820 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts


I've been looking for best (or at least good) way to handle input for the game. First result in search says I should use raw input, which I'm trying to implement now. If anyone knows any better way I'll be happy to hear it
I initialized raw input for mouse and keyboard and attempted to receive it, however I'm having some troubles with mouse clicks. I end up clicking right mouse button like 10 times before I receive data that it was pressed, after that it just gets stuck pressed down. Anyone know what I'm doing wrong?


Rid[0].usUsagePage = 0x01;
Rid[0].usUsage = 0x02;
Rid[0].dwFlags = 0;
Rid[0].hwndTarget = 0;
Rid[1].usUsagePage = 0x01;
Rid[1].usUsage = 0x06;
Rid[1].dwFlags = 0;
Rid[1].hwndTarget = 0;
if (RegisterRawInputDevices(Rid, 2, sizeof(Rid[0])) == FALSE)
Crash(L"Failed to initialize Raw Input");

Reading/Receiving input:

UINT Size;
GetRawInputBuffer(0, &Size, sizeof(RAWINPUTHEADER));
Size *= 8;
void *Buffer = _aligned_malloc(Size, 8);
UINT NumBlocks = GetRawInputBuffer((RAWINPUT*)Buffer, &Size, sizeof(RAWINPUTHEADER));
if(NumBlocks == -1)
Crash(L"Error receiving Raw Input");
RAWINPUT *RawInput = (RAWINPUT*)Buffer;
for(UINT i = 0; i < NumBlocks; ++i) {
switch(RawInput->header.dwType) {

Saving mouse button states:

void UpdateMouse(RAWMOUSE *Input) {
if(Input->usButtonFlags & RI_MOUSE_BUTTON_1_DOWN)
mMouseButtonState |= RI_MOUSE_BUTTON_1_DOWN;
if(Input->usButtonFlags & RI_MOUSE_BUTTON_1_UP)
if(mMouseButtonState & RI_MOUSE_BUTTON_1_DOWN)
mMouseButtonState ^= RI_MOUSE_BUTTON_1_DOWN;
// mouse buttons 2-5 are handled in identical way by swapping 1 to other number

Getting mouse state from outside this object:

bool GetMouseButtonState(uint8 i) {
switch(i) {
case 0:
return (mMouseButtonState & RI_MOUSE_BUTTON_1_DOWN) != 0;
case 1:
return (mMouseButtonState & RI_MOUSE_BUTTON_2_DOWN) != 0;
case 2:
return (mMouseButtonState & RI_MOUSE_BUTTON_3_DOWN) != 0;
case 3:
return (mMouseButtonState & RI_MOUSE_BUTTON_4_DOWN) != 0;
case 4:
return (mMouseButtonState & RI_MOUSE_BUTTON_5_DOWN) != 0;
return false;

Thank you in advance.

Share this post

Link to post
Share on other sites
Not sure why it doesnt work, it looks like it should. I remember not having much luck with GetRawInputBuffer(), so I use GetRawInputData() and handle the WM_INPUT message, if you dont figure you might try that.

Share this post

Link to post
Share on other sites
[color=#282828][font=helvetica, arial, verdana, tahoma, sans-serif]

GetRawInputData() inside

[/font][color=#282828][font=helvetica, arial, verdana, tahoma, sans-serif]

WM_INPUT seems to work correctly. Thank you.


Share this post

Link to post
Share on other sites
Sign in to follow this  

  • Advertisement

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!