【问题标题】:mouse_event not firing on a specific application (C#)mouse_event 未在特定应用程序上触发 (C#)
【发布时间】:2017-03-25 00:02:13
【问题描述】:

我正在尝试以编程方式模拟鼠标左键单击:

        [DllImport("user32.dll")]
        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);

        public const int MOUSEEVENTF_LEFTDOWN = 0x02;
        public const int MOUSEEVENTF_LEFTUP = 0x04;    

        public static void LeftMouseClick(int xpos, int ypos)
        {
            SetCursorPos(xpos, ypos);
            mouse_event(MOUSEEVENTF_LEFTDOWN, xpos, ypos, 0, 0);
            System.Threading.Thread.Sleep(1000);
            mouse_event(MOUSEEVENTF_LEFTUP, xpos, ypos, 0, 0);
        }

光标移动到屏幕上的适当位置,但点击并未在此处触发。有什么想法吗?

编辑:我做了几个测试。我使用的方法似乎不是问题(它成功点击了蒸汽、Skype 等应用程序)。当光标位于我特别想要单击的应用程序(android 模拟器)上方时调用 click 方法时,没有任何反应。鼠标光标移动到该位置但不会点击...现在要测试另一个模拟器。

【问题讨论】:

  • 您是否可能不应该发送ORd 事件?我认为上升和下降会发生在不同的时间
  • @BradleyDotNET 我将它编辑为不同的事件,它仍然没有触发。
  • 事件被注入到有焦点的线程的队列中。那是哪个节目?伪造输入可能不是您实际问题的解决方案。即使你是打算用 SendInput 来做的。您没有阅读文档吗?
  • @DavidHeffernan 我认为您正在做某事。有焦点的程序是我的应用程序,而不是我要点击的应用程序。所以我需要事先将焦点设置到我想点击的应用程序上?

标签: c# winapi


【解决方案1】:

我为我正在处理的另一个项目编写了一个帮助程序类,它结束了鼠标事件:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;

class MouseHelper
{
    internal struct INPUT
    {
        public UInt32 Type;
        public MOUSEKEYBDHARDWAREINPUT Data;
    }
    [StructLayout(LayoutKind.Explicit)]
    internal struct MOUSEKEYBDHARDWAREINPUT
    {
        [FieldOffset(0)]
        public MOUSEINPUT Mouse;
    }

    internal struct MOUSEINPUT
    {
        public Int32 X;
        public Int32 Y;
        public UInt32 MouseData;
        public UInt32 Flags;
        public UInt32 Time;
        public IntPtr ExtraInfo;
    }

    /// <summary>
    /// Synthesizes keystrokes, mouse motions, and button clicks.
    /// </summary>
    [DllImport("user32.dll")]
    internal static extern uint SendInput(uint nInputs, [MarshalAs(UnmanagedType.LPArray), In] INPUT[] pInputs, int cbSize);
    [DllImport("user32.dll", CharSet = CharSet.Auto, CallingConvention = CallingConvention.StdCall)]
    public static extern void mouse_event(long dwFlags, long dx, long dy, long cButtons, long dwExtraInfo);

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

    public static void DoMouseClick()
    {
        var inputMouseDown = new INPUT();
        inputMouseDown.Type = 0; /// input type mouse
        inputMouseDown.Data.Mouse.Flags = 0x0002; /// left button down

        var inputMouseUp = new INPUT();
        inputMouseUp.Type = 0; /// input type mouse
        inputMouseUp.Data.Mouse.Flags = 0x0004; /// left button up

        var inputs = new INPUT[] { inputMouseDown, inputMouseUp };
        SendInput((uint)inputs.Length, inputs, Marshal.SizeOf(typeof(INPUT)));
    }

    public static void DoMouseClick2()
    {
        mouse_event(MOUSEEVENTF_LEFTDOWN | MOUSEEVENTF_LEFTUP, 0, 0, 0, 0);
    }
}

只要我想点击鼠标,我需要做的就是调用 MouseHelper.DoMouseClick()。我怀疑可以添加鼠标移动功能作为增强功能。

【讨论】:

  • 我试过你的课,谢谢。它实际上似乎工作正常,问题是点击不会在我想要点击的特定应用程序(android 模拟器)上触发,其他一切正常
  • @binbin 你发现了吗?
猜你喜欢
  • 2017-08-09
  • 1970-01-01
  • 2018-04-15
  • 1970-01-01
  • 2013-10-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多