public extern static Int32 GetRawInputData([In] Int32 hRawInput, [In] Int32 uiCommand, [In, Out] ref RAWINPUT rawInput, [In, Out] ref Int32 pcbSize, [In] Int32 cbSizeHeader);
/// <summary> /// Updates the status of the mouse given a raw mouse handle. /// </summary> /// <param name="dHandle"></param> public void UpdateRawMouse(IntPtr dHandle) { Int32 hRawInput = dHandle.ToInt32(); Int32 pcbSize = 0; //get the size of the raw input struct Int32 retval = GetRawInputData(hRawInput, RawInput.RID_INPUT, IntPtr.Zero, ref pcbSize, Marshal.SizeOf(typeof(RAWINPUTHEADER))); //get the RAWINPUT structure. RAWINPUT ri = new RAWINPUT(); retval = GetRawInputData(hRawInput, RawInput.RID_INPUT, ref ri, ref pcbSize, Marshal.SizeOf(typeof(RAWINPUTHEADER))); foreach (RawMouse mouse in Mice) { if (mouse.Handle.ToInt32() == (Int32)ri.header.hDevice) { //Console.WriteLine("usflags: " + ri.mouse.usFlags + " button data: " + ri.mouse.usButtonData); //relative mouse mouse.X += ri.mouse.lLastX; mouse.Y += ri.mouse.lLastY; //mouse buttons if ((ri.mouse.usButtonFlags & RI_MOUSE_BUTTON_1_DOWN) > 0) { mouse.Buttons[0] = true; } if ((ri.mouse.usButtonFlags & RI_MOUSE_BUTTON_1_UP) > 0) { mouse.Buttons[0] = false; } if ((ri.mouse.usButtonFlags & RI_MOUSE_BUTTON_2_DOWN) > 0) { mouse.Buttons[1] = true; } if ((ri.mouse.usButtonFlags & RI_MOUSE_BUTTON_2_UP) > 0) { mouse.Buttons[1] = false; } if ((ri.mouse.usButtonFlags & RI_MOUSE_BUTTON_3_DOWN) > 0) { mouse.Buttons[2] = true; } if ((ri.mouse.usButtonFlags & RI_MOUSE_BUTTON_3_UP) > 0) { mouse.Buttons[2] = false; } //mouse wheel if ((ri.mouse.usButtonFlags & RI_MOUSE_WHEEL) > 0) { if ((short)ri.mouse.usButtonData > 0) { mouse.Z++; } if ((short)ri.mouse.usButtonData < 0) { mouse.Z--; } } } } }
/// <summary> /// Updates the status of the mouse given a raw mouse handle /// </summary> /// <param name="dHandle"></param> public void UpdateRawMouse(IntPtr dHandle) { //Int32 hRawInput = dHandle.ToInt32(); Int32 pcbSize = 0; // Call GetRawInputData with IntPtr.Zero to populate pcbSize with the size of the buffer required for the raw input Int32 retval = GetRawInputData(dHandle, RawInput.RID_INPUT, IntPtr.Zero, ref pcbSize, Marshal.SizeOf(typeof(RAWINPUTHEADER))); // Now call GetRawInputData again, but pass the ri RAWINPUT structure to be populated with data from the device RAWINPUT ri = new RAWINPUT(); retval = GetRawInputData(dHandle, RawInput.RID_INPUT, ref ri, ref pcbSize, Marshal.SizeOf(typeof(RAWINPUTHEADER))); // Loop through the arrayList of Mice and update the appropriate mouse with the new data received foreach (RawMouse mouse in Mice) { if (mouse.Handle == ri.header.hDevice) { //Console.WriteLine("usflags: " + ri.data.mouse.usFlags + " button data: " + ri.data.mouse.usButtonData); // Relative mouse movement mouse.X += ri.data.mouse.lLastX; mouse.Y += ri.data.mouse.lLastY; // Mouse buttons if ((ri.data.mouse.usButtonFlags & RI_MOUSE_BUTTON_1_DOWN) > 0) { mouse.Buttons[0] = true; } if ((ri.data.mouse.usButtonFlags & RI_MOUSE_BUTTON_1_UP) > 0) { mouse.Buttons[0] = false; } if ((ri.data.mouse.usButtonFlags & RI_MOUSE_BUTTON_2_DOWN) > 0) { mouse.Buttons[1] = true; } if ((ri.data.mouse.usButtonFlags & RI_MOUSE_BUTTON_2_UP) > 0) { mouse.Buttons[1] = false; } if ((ri.data.mouse.usButtonFlags & RI_MOUSE_BUTTON_3_DOWN) > 0) { mouse.Buttons[2] = true; } if ((ri.data.mouse.usButtonFlags & RI_MOUSE_BUTTON_3_UP) > 0) { mouse.Buttons[2] = false; } // Scroll wheel if ((ri.data.mouse.usButtonFlags & RI_MOUSE_WHEEL) > 0) { if ((short)ri.data.mouse.usButtonData > 0) { mouse.Z++; } if ((short)ri.data.mouse.usButtonData < 0) { mouse.Z--; } } } } }