Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- private void ProcessInputCommand(Message message)
- {
- uint dwSize = 0;
- // First call to GetRawInputData sets the value of dwSize,
- // which can then be used to allocate the appropriate amount of memory,
- // storing the pointer in "buffer".
- UnsafeNativeMethods.GetRawInputData(message.LParam,
- UnsafeNativeMethods.RID_INPUT, IntPtr.Zero,
- ref dwSize,
- (uint)Marshal.SizeOf(typeof(RAWINPUTHEADER)));
- IntPtr buffer = Marshal.AllocHGlobal((int)dwSize);
- try
- {
- // Check that buffer points to something, and if so,
- // call GetRawInputData again to fill the allocated memory
- // with information about the input
- if (buffer != IntPtr.Zero &&
- UnsafeNativeMethods.GetRawInputData(message.LParam,
- UnsafeNativeMethods.RID_INPUT,
- buffer,
- ref dwSize,
- (uint)Marshal.SizeOf(typeof(RAWINPUTHEADER))) == dwSize)
- {
- // Store the message information in "raw", then check
- // that the input comes from a keyboard device before
- // processing it to raise an appropriate KeyPressed event.
- RAWINPUT raw = (RAWINPUT)Marshal.PtrToStructure(buffer, typeof(RAWINPUT));
- if (raw.header.dwType == UnsafeNativeMethods.RIM_TYPEKEYBOARD)
- {
- // Filter for Key Down events and then retrieve information
- // about the keystroke
- if (raw.keyboard.Message == UnsafeNativeMethods.WM_KEYDOWN || raw.keyboard.Message == UnsafeNativeMethods.WM_SYSKEYDOWN)
- {
- ushort key = raw.keyboard.VKey;
- }
- [DllImport("User32.dll")]
- extern internal static uint GetRawInputData(IntPtr hRawInput, uint uiCommand, IntPtr pData, ref uint pcbSize, uint cbSizeHeader);
- /// <summary>
- /// Contains the raw input from a device.
- /// </summary>
- [StructLayout(LayoutKind.Sequential)]
- public struct RawInput
- {
- /// <summary>
- /// Header for the data.
- /// </summary>
- public RawInputHeader Header;
- public Union Data;
- [StructLayout(LayoutKind.Explicit)]
- public struct Union
- {
- /// <summary>
- /// Mouse raw input data.
- /// </summary>
- [FieldOffset(0)]
- public RawMouse Mouse;
- /// <summary>
- /// Keyboard raw input data.
- /// </summary>
- [FieldOffset(0)]
- public RawKeyboard Keyboard;
- /// <summary>
- /// HID raw input data.
- /// </summary>
- [FieldOffset(0)]
- public RawHID HID;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement