Jump to content
  • Advertisement
Sign in to follow this  
Tape_Worm

[.net] WM_INPUT

This topic is 4456 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

Has anyone ever tried to use WM_INPUT with .NET? Anyone actually get something working? I've managed to get a raw device registered with RegisterRawInputDevices(). But when I call GetRawInputData() I get a return code of -1 (error). The function declaration is as follows:
[DllImport("user32.dll")]
public static extern int GetRawInputData(IntPtr hRawInput, RawInputCommand uiCommand, ref RAWINPUT pData, ref int pcbSize, int cbSizeHeader);

	/// <summary>
	/// Value type for raw input devices.
	/// </summary>
	[StructLayout(LayoutKind.Sequential)]
	public struct RAWINPUTDEVICE
	{
		/// <summary>Top level collection Usage page for the raw input device.</summary>
		public short UsagePage;
		/// <summary>Top level collection Usage for the raw input device. </summary>
		public short Usage;
		/// <summary>Mode flag that specifies how to interpret the information provided by UsagePage and Usage.</summary>
		public RawInputDeviceFlags Flags;
		/// <summary>Handle to the target device. If NULL, it follows the keyboard focus.</summary>
		public IntPtr WindowHandle;
	}

	/// <summary>
	/// Value type for a raw input header.
	/// </summary>
	[StructLayout(LayoutKind.Sequential)]
	public struct RAWINPUTHEADER
	{
		/// <summary>Type of device the input is coming from.</summary>
		public RawInputType Type;
		/// <summary>Size of the packet of data.</summary>
		public int Size;
		/// <summary>Handle to the device sending the data.</summary>
		public IntPtr Device;
		/// <summary>wParam from the window message.</summary>
		public IntPtr wParam;
	}

	/// <summary>
	/// Value type for raw input from a mouse.
	/// </summary>
	[StructLayout(LayoutKind.Sequential)]
	public struct RAWINPUTMOUSE
	{
		/// <summary>Flags for the event.</summary>
		public RawMouseFlags Flags;
		/// <summary>Flags for the event.</summary>
		public RawMouseButtons Buttons;
		/// <summary>If the mouse wheel is moved, this will contain the delta amount.</summary>
		public short ButtonData;
		/// <summary>Raw button data.</summary>
		public int RawButtons;
		/// <summary>Relative direction of motion, depending on flags.</summary>
		public int LastX;
		/// <summary>Relative direction of motion, depending on flags.</summary>
		public int LastY;
		/// <summary>Extra information.</summary>
		public int ExtraInformation;
	}

	/// <summary>
	/// Value type for raw input from a keyboard.
	/// </summary>	
	[StructLayout(LayoutKind.Sequential)]
	public struct RAWINPUTKEYBOARD
	{
		/// <summary>Scan code for key depression.</summary>
		public short MakeCode;
		/// <summary>Scan code information.</summary>
		public RawKeyboardFlags Flags;
		/// <summary>Reserved.</summary>
		public short Reserved;
		/// <summary>Virtual key code.</summary>
		public VirtualKeys VirtualKey;
		/// <summary>Corresponding window message.</summary>
		public WindowMessages Message;
		/// <summary>Extra information.</summary>
		public int ExtraInformation;
	}

	/// <summary>
	/// Value type for raw input from a HID.
	/// </summary>	
	[StructLayout(LayoutKind.Sequential)]
	public struct RAWINPUTHID
	{
		/// <summary>Size of the HID data in bytes.</summary>
		public int Size;
		/// <summary>Number of HID in Data.</summary>
		public int Count;
		/// <summary>Data for the HID.</summary>
		public IntPtr Data;
	}

	/// <summary>
	/// Value type for raw input.
	/// </summary>
	[StructLayout(LayoutKind.Explicit, Pack=1)]
	public struct RAWINPUT
	{
		/// <summary>Header for the data.</summary>
		[FieldOffset(0)]
		public RAWINPUTHEADER Header;
		/// <summary>Mouse raw input data.</summary>
		[FieldOffset(16)]
		public RAWINPUTMOUSE Mouse;
		/// <summary>Keyboard raw input data.</summary>
		[FieldOffset(16)]
		public RAWINPUTKEYBOARD Keyboard;
		/// <summary>HID raw input data.</summary>
		[FieldOffset(16)]
		public RAWINPUTHID HID;
	}

I'm not 100% sure if I have this set up correctly or not. My pinvoke crap is sad at best. If you can help, or have gotten it working I'd appreciate some help.

Share this post


Link to post
Share on other sites
Advertisement
I'm trying to capture raw mouse input. But I'm not sure if my pinvoke is set up correctly (there's nothing on the pinvoke site regarding this) or if I'm not intercepting the message correctly:


protected override void WndProc(ref Message m)
{
if (m.WParam.ToInt32() == (int)WindowMessages.RawInput)
{
int size = Marshal.SizeOf(typeof(RAWINPUT));
int outSize = 0;
RAWINPUT input = new RAWINPUT();

outSize = Win32API.GetRawInputData(m.LParam, RawInputCommand.Input, ref input, ref size, System.Runtime.InteropServices.Marshal.SizeOf(typeof(RAWINPUTHEADER)));
if (outSize != -1)
{
if (input.Header.Type == RawInputType.Mouse)
{
p.X = input.Mouse.LastX;
p.Y = input.Mouse.LastY;
Invalidate();
}
}
m.Result = IntPtr.Zero;
return;
}
base.WndProc(ref m);
}

Share this post


Link to post
Share on other sites
Quote:
Original post by Dave Hunt
Did you call RegisterRawInputDevices()?


Quote:
Original post by Tape_Worm
I've managed to get a raw device registered with RegisterRawInputDevices(). But when I call GetRawInputData() I get a return code of -1 (error).


Share this post


Link to post
Share on other sites
Goddamnit. I'm dumb.

I was intercepting the wParam of the message, not the message itself. It's been far too long since I've done message handling that way. Now it works beautifully.

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!