尝试模拟鼠标单击/拖动

发布于 2024-12-22 01:11:26 字数 1405 浏览 0 评论 0 原文

所以我试图模拟鼠标左键单击和鼠标左键释放来进行一些自动拖放。

它目前在 C# Winforms 中(是的,winforms :|)并且有点笨拙。

基本上,一旦发送“点击”,我希望它根据 Kinect 输入更新光标位置。 Kinect 方面的事情很好,但我不确定如何确定按钮是否仍然按下。

这是我当前正在使用的代码+一些伪代码,以帮助更好地解释自己(do while)。

class MouseImpersonator
{
    [DllImport("user32.dll", CharSet = CharSet.Auto, CallingConvention = CallingConvention.StdCall)]
    private static extern void mouse_event(uint dwFlags, uint dx, uint dy, uint cButtons, uint dwExtraInfo);

    private const int leftDown = 0x02;
    private const int leftUp = 0x04;

    public static void Grab(int xPos, int yPos)
    {
        Cursor.Position = new Point(xPos + 25, yPos + 25);
        mouse_event(leftDown, (uint) xPos, (uint) yPos, 0, 0);

        //do
        //{
        //Cursor.Position = new Point(KinectSettings.movement.LeftHandX, KinectSettings.movement.LeftHandY);
        //} while (the left mouse button is still clicked);
    }

    public static void Release(int xPos, int yPos)
    {
        Cursor.Position = new Point(xPos + 25, yPos + 25);
        mouse_event(leftUp, (uint) xPos, (uint) yPos, 0, 0);
    }
}

我在 google 上搜索了一下,除了 WPF 等效项之外找不到任何我需要的东西: http://msdn.microsoft.com/en-us/library/system.windows.input.mouse.aspx

我有点超出了我的深度,但任何帮助都是非常感谢。

卢卡斯.

    -

So I'm trying to simulate the left mouse click and the left mouse release to do some automated dragging and dropping.

It's currently in a C# Winforms (Yes, winforms :|) and is being a bit of a goose.

Basically, once a Click is sent, I want it to update the cursor position based upon the Kinect input. The Kinect side of things is fine but i'm not sure how to find if the button is still pressed or not.

here's the code i'm currently using + some psuedocode to help better explain myself (the do while).

class MouseImpersonator
{
    [DllImport("user32.dll", CharSet = CharSet.Auto, CallingConvention = CallingConvention.StdCall)]
    private static extern void mouse_event(uint dwFlags, uint dx, uint dy, uint cButtons, uint dwExtraInfo);

    private const int leftDown = 0x02;
    private const int leftUp = 0x04;

    public static void Grab(int xPos, int yPos)
    {
        Cursor.Position = new Point(xPos + 25, yPos + 25);
        mouse_event(leftDown, (uint) xPos, (uint) yPos, 0, 0);

        //do
        //{
        //Cursor.Position = new Point(KinectSettings.movement.LeftHandX, KinectSettings.movement.LeftHandY);
        //} while (the left mouse button is still clicked);
    }

    public static void Release(int xPos, int yPos)
    {
        Cursor.Position = new Point(xPos + 25, yPos + 25);
        mouse_event(leftUp, (uint) xPos, (uint) yPos, 0, 0);
    }
}

I've had a hunt of the google and can't find anything for what I need except for a WPF equivalent: http://msdn.microsoft.com/en-us/library/system.windows.input.mouse.aspx

I'm a bit out of my depth, but any help is greatly appreciated.

Lucas.

    -

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

伤感在游骋 2024-12-29 01:11:26

最简单的答案实际上是使用 bool 并检查发生了什么。

我在一个新线程上启动它,因此它不会破坏其他所有内容。

理想情况下,你应该稍微整理一下。

    public static void Grab(int xPos, int yPos)
    {
        _dragging = true;

        Cursor.Position = new Point(xPos, yPos + offSet);
        mouse_event(leftDown, (uint) xPos, (uint) yPos, 0, 0);

        var t = new Thread(CheckMouseStatus);
        t.Start();
    }
    public static void Release(int xPos, int yPos)
    {
        _dragging = false;
        Cursor.Position = new Point(xPos, yPos + offSet);
        mouse_event(leftUp, (uint) xPos, (uint) yPos, 0, 0);
    }

    private static void CheckMouseStatus()
    {
        do
        {
            Cursor.Position = new Point(KinectSettings.movement.HandX, KinectSettings.movement.HandY + offSet);
        } 
        while (_dragging);
    }

The Easiest answer was infact to use a bool and just check to see what's going on.

I started it on a new thread so it didn't break everything else.

Idealy you'd tidy this up a little bit.

    public static void Grab(int xPos, int yPos)
    {
        _dragging = true;

        Cursor.Position = new Point(xPos, yPos + offSet);
        mouse_event(leftDown, (uint) xPos, (uint) yPos, 0, 0);

        var t = new Thread(CheckMouseStatus);
        t.Start();
    }
    public static void Release(int xPos, int yPos)
    {
        _dragging = false;
        Cursor.Position = new Point(xPos, yPos + offSet);
        mouse_event(leftUp, (uint) xPos, (uint) yPos, 0, 0);
    }

    private static void CheckMouseStatus()
    {
        do
        {
            Cursor.Position = new Point(KinectSettings.movement.HandX, KinectSettings.movement.HandY + offSet);
        } 
        while (_dragging);
    }
猫烠⑼条掵仅有一顆心 2024-12-29 01:11:26
[DllImport("user32.dll", CharSet = CharSet.Auto, CallingConvention = CallingConvention.StdCall)]
    private static extern void mouse_event(uint dwFlags, int dx, int dy, uint cButtons, uint dwExtraInfo);

    [DllImport("user32.dll")]
    static extern bool SetCursorPos(int X, int Y);

    const uint MOUSEEVENTF_LEFTDOWN = 0x0002;
    const uint MOUSEEVENTF_LEFTUP = 0x0004;
    const uint MOUSEEVENTF_MOVE = 0x0001;

    static void Drag(int startX,int startY,int endX,int endY)
    {
        endX = endX - startX;
        endY = endY - startY;
        SetCursorPos(startX, startY);
        mouse_event(MOUSEEVENTF_LEFTDOWN, 0, 0, 0, 0);
        mouse_event(MOUSEEVENTF_MOVE, endX, endY, 0, 0);
        mouse_event(MOUSEEVENTF_LEFTUP, 0, 0, 0, 0);
    }
[DllImport("user32.dll", CharSet = CharSet.Auto, CallingConvention = CallingConvention.StdCall)]
    private static extern void mouse_event(uint dwFlags, int dx, int dy, uint cButtons, uint dwExtraInfo);

    [DllImport("user32.dll")]
    static extern bool SetCursorPos(int X, int Y);

    const uint MOUSEEVENTF_LEFTDOWN = 0x0002;
    const uint MOUSEEVENTF_LEFTUP = 0x0004;
    const uint MOUSEEVENTF_MOVE = 0x0001;

    static void Drag(int startX,int startY,int endX,int endY)
    {
        endX = endX - startX;
        endY = endY - startY;
        SetCursorPos(startX, startY);
        mouse_event(MOUSEEVENTF_LEFTDOWN, 0, 0, 0, 0);
        mouse_event(MOUSEEVENTF_MOVE, endX, endY, 0, 0);
        mouse_event(MOUSEEVENTF_LEFTUP, 0, 0, 0, 0);
    }
嗫嚅 2024-12-29 01:11:26

如果鼠标左键按下,则以下代码应返回 true;如果按下,则返回 false,Control 为 System.Windows.Forms.Control:

    Control.MouseButtons.HasFlag(MouseButtons.Left)

可以找到此内容的 ps 文档 在 MSDN 上

The following code should return true if the left mouse button is down, false if it is up, Control being System.Windows.Forms.Control:

    Control.MouseButtons.HasFlag(MouseButtons.Left)

p.s. documentation for this can be found on MSDN here.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文