Jump to content

  • Log In with Google      Sign In   
  • Create Account


Receiving Raw Input problem


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
2 replies to this topic

#1 Ripiz   Members   -  Reputation: 529

Like
0Likes
Like

Posted 01 February 2012 - 01:23 PM

Hello,

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?

Initialization:
  RAWINPUTDEVICE Rid[2];
  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));
  if(!Size)
   return;
  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) {
    case RIM_TYPEMOUSE:
	 UpdateMouse((RAWMOUSE*)RawInput->data.hid.bRawData);
	 break;
    case RIM_TYPEKEYBOARD:
	 UpdateKeyboard((RAWKEYBOARD*)RawInput->data.hid.bRawData);
	 break;
    default:
	 break;
   }
   NEXTRAWINPUTBLOCK(RawInput);
  }
  _aligned_free(Buffer);

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;
   default:
    return false;
  }
}

Thank you in advance.

Sponsor:

#2 NumberXaero   Prime Members   -  Reputation: 1398

Like
0Likes
Like

Posted 01 February 2012 - 02:32 PM

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.

#3 Ripiz   Members   -  Reputation: 529

Like
0Likes
Like

Posted 02 February 2012 - 11:13 AM

GetRawInputData() inside

WM_INPUT seems to work correctly. Thank you.






Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS