来自 User32.dll 的 UWP mouse_event 函数请勿点击

UWP mouse_event function from User32.dll do not click

我在 WPF 中有一个运行良好的应用程序(用于测试的输入模拟器),出于某些原因我必须将它转换为 UWP 版本 18362。

我发现 User32.dll 中的 mouse_event 不起作用。我可以将鼠标移到 window 之外,但我无法点击任何地方。当 window 被卸载时,事件移动就不会工作。有什么我缺少的功能吗?

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

[DllImport("user32.dll", CharSet = CharSet.Auto, CallingConvention = CallingConvention.StdCall)]
public static extern void mouse_event(uint dwFlags, uint dx, uint dy, uint cButtons, UIntPtr dwExtraInfo);
private const uint MOUSEEVENTF_LEFTDOWN = 0x02;
private const uint MOUSEEVENTF_LEFTUP = 0x04;
private const uint MOUSEEVENTF_RIGHTDOWN = 0x08;
private const uint MOUSEEVENTF_RIGHTUP = 0x10; 

...

        void Click(int x, int y){
            SetCursorPos(_random.Next(x + 0, x + 100), _random.Next(y + 0, y + 100));
            mouse_event(MOUSEEVENTF_RIGHTDOWN | MOUSEEVENTF_RIGHTUP, 0, 0, 0, new UIntPtr());
        }

结果是,鼠标移动到想要的位置,但没有点击。

这是 mouse_event 方法在 UWP 应用程序中不起作用的预期行为。因为根据this article,我们可以看到mouse_event只在桌面应用程序中支持,但在UWP应用程序中不支持。

>>我的 WPF 应用运行良好(用于测试的输入模拟器)

如果想在UWP中实现模拟用户输入,请查看以下文章:

通过输入注入模拟用户输入:

https://docs.microsoft.com/en-us/windows/uwp/design/input/input-injection.