Sign in to follow this  
Tape_Worm

[.net] WM_INPUT

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
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

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this