如何移动光标或模拟点击其他应用程序?光标、应用程序

2023-09-02 10:33:50 作者:庸人自扰、

我创建使用大跃进运动控制器一个C#Windows应用程序。我开发的Windows 8和Visual Studio 2010中我使用 SetCursorPos mouse_event USER32 .DLL 移动光标和模拟点击。

I am creating a C# Windows application using the Leap Motion Controller. I am developing on Windows 8 and Visual Studio 2010. I use SetCursorPos and mouse_event from user32.dll to move the cursor and simulate clicks.

我希望光标时,在任何应用程序中移动。当我从Visual Studio运行/调试它也只有在应用程序本身或Visual Studio工作。当其他应用程序的鼠标不移动,点击不工作,但如果我尝试将光标移动与实际鼠标它返回到它在的地方。当独立运行它不会移动在Visual Studio和鼠标可以与真正的鼠标在其他应用中移动。

I want the cursor to be moved when in any application. When I run/debug it from Visual Studio it only works when in the application itself or in Visual Studio. When in other applications the mouse does not move and the clicks don't work, but if I try to move the cursor with the real mouse it goes back to the place it was at. When run standalone it does not move in Visual Studio and the mouse can be moved with the real mouse in other applications.

我有这个code使用 SetCursorPos mouse_event

I have this code to use SetCursorPos and mouse_event:

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

[DllImport("User32.Dll")]
public static extern void mouse_event(uint dwFlags, uint dx, uint dy, uint cButtons, uint dwExtraInfo);

public static void MouseClick(uint x, uint y) {
    mouse_event(MOUSEEVENTF_LEFTDOWN | MOUSEEVENTF_LEFTUP, x, y, 0, 0);
}

在我的表单类我都存储在两个领域中的鼠标位置。在定时器的蜱我设置光标位置,并做必要的点击这样的:

In my form class I have the mouse location stored in two fields. In a Timer's tick I set the cursor position and do necessary clicks like this:

if (!frame.Hands.IsEmpty) {
    Hand hand = frame.Hands.Leftmost;
    if (!hand.Fingers.IsEmpty) {
        // Get coordinates...
        SetCursorPos(mousex, mousey);
    }
}
foreach (Gesture gesture : gestures) {
    if (gesture.Type == Gesture.GestureType.TYPESCREENTAP) {
        MouseClick(mousex, mousey);
    }
}

如果语句是大跃进设备;我想移动鼠标和做其他的东西时,才会有一个拿到手的坐标。

The if statements are for the Leap device; I want to move the mouse and do other stuff only when there is a hand to get the coordinates.

它甚至有可能将光标移动或模拟鼠标点击其他应用程序?而如果是,怎么了?

Is it even possible to move the cursor or simulate mouse clicks for other applications? And if it is, how?

推荐答案

我设法解决自己的问题。

I managed to solve the problem myself.

有两个问题。第一个(主)之一是,大跃进设备未发送帧到我的应用程序时,它在后台运行。作为建议的的这里

There were two problems. The first (and the main) one was that the Leap device was not sending frames to my application when it was in the background. That was fixed by adding this code to the listener's onConnect() method, as suggested here:

controller.SetPolicyFlags(Controller.PolicyFlag.POLICYBACKGROUNDFRAMES);

的第二个问题是,独立运行的应用程序时没有获得输入发送给其他应用程序。我跟着发现说明 这里 (向下滚动,有关于信息 uiAccess )和 这里 并签署了我的申请,并已将此添加清单:

The second problem was that when run standalone the application didn't have access to send input to other applications. I followed instructions found here (scroll down, there's info about uiAccess) and here and signed my application and added this to the manifest:

<requestedExecutionLevel level="asInvoker" uiAccess="true" />
 
精彩推荐
图片推荐