Advertisement
GAMELASTER

Untitled

Jun 21st, 2019
1,308
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #pragma once
  2.  
  3. namespace rawinput
  4. {
  5.     typedef BOOL(WINAPI* fPeekMessage)(
  6.         _Out_     LPMSG lpMsg,
  7.         _In_opt_  HWND hWnd,
  8.         _In_      UINT wMsgFilterMin,
  9.         _In_      UINT wMsgFilterMax,
  10.         _In_      UINT wRemoveMsg
  11.         );
  12.  
  13.     BOOL WINAPI PeekMessage_Hook(
  14.         _Out_     LPMSG lpMsg,
  15.         _In_opt_  HWND hWnd,
  16.         _In_      UINT wMsgFilterMin,
  17.         _In_      UINT wMsgFilterMax,
  18.         _In_      UINT wRemoveMsg
  19.     );
  20.  
  21.     std::function<bool(RAWMOUSE)> on_mousemove;
  22.     std::function<bool(RAWKEYBOARD)> on_keyboard;
  23.  
  24.     fPeekMessage mOriginalPeekMessage;
  25.  
  26.     inline bool ProcessMessage(LPMSG message)
  27.     {
  28.         UINT dwSize;
  29.         GetRawInputData((HRAWINPUT)message->lParam, RID_INPUT, NULL, &dwSize, sizeof(RAWINPUTHEADER));
  30.  
  31.         LPBYTE lpb = new BYTE[dwSize];
  32.  
  33.         if (lpb)
  34.         {
  35.             if (GetRawInputData((HRAWINPUT)message->lParam, RID_INPUT, lpb, &dwSize, sizeof(RAWINPUTHEADER)) == dwSize)
  36.             {
  37.                 RAWINPUT* raw = (RAWINPUT*)lpb;
  38.  
  39.                 if (raw->header.dwType == RIM_TYPEKEYBOARD)
  40.                 {
  41.                     if(on_keyboard != nullptr)
  42.                         return on_keyboard(raw->data.keyboard);
  43.                 }
  44.                 else if (raw->header.dwType == RIM_TYPEMOUSE)
  45.                 {
  46.                     if (on_mousemove != nullptr)
  47.                         return on_mousemove(raw->data.mouse);
  48.                 }
  49.  
  50.                 delete[] lpb;
  51.             }
  52.         }
  53.     }
  54.  
  55.     BOOL WINAPI PeekMessage_Hook(
  56.         _Out_     LPMSG lpMsg,
  57.         _In_opt_  HWND hWnd,
  58.         _In_      UINT wMsgFilterMin,
  59.         _In_      UINT wMsgFilterMax,
  60.         _In_      UINT wRemoveMsg
  61.     )
  62.     {
  63.         bool bResult = mOriginalPeekMessage(lpMsg, hWnd, wMsgFilterMin, wMsgFilterMax, wRemoveMsg) == 1;
  64.         bool doInterupt = false;
  65.         if (bResult)
  66.         {
  67.             switch (lpMsg->message)
  68.             {
  69.             case WM_INPUT:
  70.             {
  71.                 doInterupt = ProcessMessage(lpMsg);
  72.                 break;
  73.             }
  74.             default:
  75.                 break;
  76.             }
  77.         }
  78.  
  79.        
  80.         return bResult && !doInterupt;
  81.     }
  82.  
  83.     inline void hook()
  84.     {
  85.         mOriginalPeekMessage = reinterpret_cast <fPeekMessage> (DetourFunction(DetourFindFunction("USER32.DLL", "PeekMessageW"), reinterpret_cast < PBYTE > (PeekMessage_Hook)));
  86.     }
  87. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement