Advertisement
Guest User

Untitled

a guest
Feb 20th, 2019
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.76 KB | None | 0 0
  1. private void ProcessInputCommand(Message message)
  2. {
  3. uint dwSize = 0;
  4.  
  5. // First call to GetRawInputData sets the value of dwSize,
  6. // which can then be used to allocate the appropriate amount of memory,
  7. // storing the pointer in "buffer".
  8. UnsafeNativeMethods.GetRawInputData(message.LParam,
  9. UnsafeNativeMethods.RID_INPUT, IntPtr.Zero,
  10. ref dwSize,
  11. (uint)Marshal.SizeOf(typeof(RAWINPUTHEADER)));
  12.  
  13. IntPtr buffer = Marshal.AllocHGlobal((int)dwSize);
  14. try
  15. {
  16. // Check that buffer points to something, and if so,
  17. // call GetRawInputData again to fill the allocated memory
  18. // with information about the input
  19. if (buffer != IntPtr.Zero &&
  20. UnsafeNativeMethods.GetRawInputData(message.LParam,
  21. UnsafeNativeMethods.RID_INPUT,
  22. buffer,
  23. ref dwSize,
  24. (uint)Marshal.SizeOf(typeof(RAWINPUTHEADER))) == dwSize)
  25. {
  26. // Store the message information in "raw", then check
  27. // that the input comes from a keyboard device before
  28. // processing it to raise an appropriate KeyPressed event.
  29.  
  30. RAWINPUT raw = (RAWINPUT)Marshal.PtrToStructure(buffer, typeof(RAWINPUT));
  31.  
  32. if (raw.header.dwType == UnsafeNativeMethods.RIM_TYPEKEYBOARD)
  33. {
  34. // Filter for Key Down events and then retrieve information
  35. // about the keystroke
  36. if (raw.keyboard.Message == UnsafeNativeMethods.WM_KEYDOWN || raw.keyboard.Message == UnsafeNativeMethods.WM_SYSKEYDOWN)
  37. {
  38. ushort key = raw.keyboard.VKey;
  39. }
  40.  
  41. [DllImport("User32.dll")]
  42. extern internal static uint GetRawInputData(IntPtr hRawInput, uint uiCommand, IntPtr pData, ref uint pcbSize, uint cbSizeHeader);
  43.  
  44. /// <summary>
  45. /// Contains the raw input from a device.
  46. /// </summary>
  47. [StructLayout(LayoutKind.Sequential)]
  48. public struct RawInput
  49. {
  50. /// <summary>
  51. /// Header for the data.
  52. /// </summary>
  53. public RawInputHeader Header;
  54. public Union Data;
  55. [StructLayout(LayoutKind.Explicit)]
  56. public struct Union
  57. {
  58. /// <summary>
  59. /// Mouse raw input data.
  60. /// </summary>
  61. [FieldOffset(0)]
  62. public RawMouse Mouse;
  63. /// <summary>
  64. /// Keyboard raw input data.
  65. /// </summary>
  66. [FieldOffset(0)]
  67. public RawKeyboard Keyboard;
  68. /// <summary>
  69. /// HID raw input data.
  70. /// </summary>
  71. [FieldOffset(0)]
  72. public RawHID HID;
  73. }
  74. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement