0

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.

JY Lee
  • 45
  • 6
  • 1
    It's actually the purpose of simulating a mouse click that this behaves as if the real user did it. I therefore strongly doubt this is possible. – PMF Apr 06 '22 at 08:57
  • 1
    @pmf Injected input is identifiable as such in a low-level keyboard or mouse hook. – IInspectable Apr 06 '22 at 11:04

0 Answers0