'How to check if mouse click triggered by user32.dll or real user [duplicate]


private const int WH_MOUSE_LL = 14;
public static LowLevelMouseProc _proc = HookCallback;
public static IntPtr _hookID = IntPtr.Zero;

[DllImport("user32.dll")]
public static extern bool SetCursorPos(int x, int y);

[DllImport("user32.dll")]
public static extern void mouse_event(int dwFlags, int dx, int dy, int cButtons, int dwExtraInfo);

[DllImport("kernel32.dll", CharSet = CharSet.Auto, SetLastError = true)]
public static extern IntPtr GetModuleHandle(string lpModuleName);

[DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)]
public static extern IntPtr SetWindowsHookEx(int idHook, LowLevelMouseProc lpfn, IntPtr hMod, uint dwThreadId);

[DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)]
public static extern IntPtr CallNextHookEx(IntPtr hhk, int nCode, IntPtr wParam, IntPtr lParam);

public enum MouseMessages
{
      WM_LBUTTONDOWN = 0x0201,
      WM_LBUTTONUP = 0x0202,
      WM_MOUSEMOVE = 0x0200,
      WM_MOUSEWHEEL = 0x020A,
      WM_RBUTTONDOWN = 0x0204,
      WM_RBUTTONUP = 0x0205
}

public static void SetCursorAndClick(double x, double y)
{
   SetCursorPos((int)x, (int)y);
   mouse_event(MOUSEEVENTF_LEFTDOWN, 0, 0, 0, 0);
   mouse_event(MOUSEEVENTF_LEFTUP, 0, 0, 0, 0);
}

public static IntPtr SetHook(LowLevelMouseProc proc)
{
   using (Process curProcess = Process.GetCurrentProcess())
        using (ProcessModule curModule = curProcess.MainModule)
        {
          return SetWindowsHookEx(WH_MOUSE_LL, proc, GetModuleHandle(curModule.ModuleName), 0);
        }
}

public static IntPtr HookCallback(int nCode, IntPtr wParam, IntPtr lParam)
{
  if (nCode >= 0 && MouseMessages.WM_LBUTTONUP == (MouseMessages)wParam)
  {
    // I want to check this Callback called by user32.dll mouse_event function or real user
  }

  return CallNextHookEx(_hookID, nCode, wParam, lParam);
}

void Main()
{
   SetHook(HookCallback);
}

I want to check when some mouse click event on anywhere on windows triggered this Callback of the event called by user32.dll mouse_event in SetCursorAndClick function or real user`s real mouse button click.

see HookCallback Function please. When some click event triggered then raise Callback function HookCallback.



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source