Search code examples
c#uwpraspberry-piwin-universal-appwindowsiot

UWP: Simulate click at specific coordinates on Windows IoT


Is there a way to simulate a click at specific coordinates on Windows IoT?

I tried with mouse_event:

mouse_event(MOUSEEVENTF_LEFTDOWN, x, y, 0, 0);

but, I get this error:

System.EntryPointNotFoundException: 'Unable to find an entry point named 'mouse_event' in DLL 'user32.dll'.'. 

Is it because that function doesn't exist in the IoT version of Windows?

I saw there was SendInput, but the only syntax on the documentation is in C++. Is it possible at all to use it in C# on Windows IoT and if so how? If you have an example in mind, linking it would be very helpful. I searched around but I couldn't find something that could work on UWP.

Here is the code I used for the mouse_event:

[System.Runtime.InteropServices.DllImport("user32.dll", EntryPoint = "SetCursorPos")]
private static extern bool SetCursorPos(int X, int Y);

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

public const int MOUSEEVENTF_LEFTDOWN = 0x02;
public const int MOUSEEVENTF_LEFTUP = 0x04;
public const int MOUSEEVENTF_RIGHTDOWN = 0x08;
public const int MOUSEEVENTF_RIGHTUP = 0x10;

//...

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

Solution

  • Neither mouse_event nor SendInput can be used in a UWP application. As documented, these API's are for

    desktop apps only

    If you are running this from a UWP application (which apparently you are), there is no way to automate another UWP application. The sandboxing will not allow this. This includes UI Automation.