拦截鼠标点击 [英] Intercept Mouse Click

查看:49
本文介绍了拦截鼠标点击的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在编写一个程序,该程序在最初单击时按住鼠标按钮,并继续按住它直到用户第二次按下鼠标按钮.

I'm currently writing a program that holds down a mouse button when initially clicked, and continues to hold it until the user presses the mouse button for a second time.

该程序的工作原理是使用 MouseHookListener 全局检测输入,然后使用 输入模拟器 按住已按下的鼠标按钮.

The program works by detecting input globally using a MouseHookListener and then uses an input simulator to hold down the mouse button that has been pressed.

程序能够按预期按住鼠标,但原始鼠标单击存在问题,该问题指示程序模拟按住的按钮;它仍然被执行.我知道 MouseHookListener 使用低级钩子来操作,但它的 HookCallBack() 方法由于受到保护而不能被覆盖.

The program is able to hold down the mouse as intended, but there is an issue with the original mouse click that signals the program to simulate the button being held; it still gets carried out. I know that the MouseHookListener uses low level hooks to operate, but it's HookCallBack() method can't be overridden due to it being protected.

有什么办法可以屏蔽掉原来的鼠标输入?或者有没有办法保持原始输入直到再次单击鼠标?

Is there any way to block out the original mouse input? Or is there a way to make the original input held in until the mouse is clicked once more?

到目前为止,这是我生成的代码(注意 - mListener 正在其他论坛中被激活):

This is the code I've produced, thus far (note - the mListener is being activated in a forum else where):

    public MouseHold()
    {
        mListener = new MouseHookListener(new GlobalHooker());
        mListener.MouseClick += mListener_MouseClick;
    }

    private bool isDown;
    private int count = 0;
    private InputSimulator sim = new InputSimulator();

    public MouseHookListener mListener;        

    private void mListener_MouseClick(object sender, MouseEventArgs e)
    {
        if (e.Button == System.Windows.Forms.MouseButtons.Left)
        {
            if (isDown)
            {
                isDown = false;                    
                Console.Out.WriteLine(count);
            }

            else
            {
                Console.Out.WriteLine(count);
                isDown = true;
                sim.Mouse.LeftButtonDown();

            }
        }
    }

推荐答案

我是你提到的图书馆的作者 https://github.com/gmamaladze/globalmousekeyhook.

I am the author of the library you mentioned https://github.com/gmamaladze/globalmousekeyhook.

为了启用您的用例,我又通过一个事件扩展了库.请注意,新版本中监听器的构造也略有变化.

To enable your usecase I have extended the library by one more event. Please note that the construction of listener has also slightly changed in the new version.

我相信通过下面的示例,您可以在不涉及 InputSimulator 库的情况下完成您的场景.

I beleave that with the example below you are able to accomplish your scenario without involving InputSimulator library.

  1. 第一次按下鼠标设置标志 m_SupressNextUp - 向下触发.
  2. 上一次按下后的向上鼠标按钮被抑制并设置标志m_SupressNextDown
  3. 第二下也被抑制并重置标志m_SupressNextUp
  4. 第二次启动并重置m_SupressNextDown
  5. 系统返回初始状态.


internal class Sample
{
    private IKeyboardMouseEvents m_GlobalHook;
    private bool m_SupressNextUp;
    private bool m_SupressNextDown;

    public void Subscribe()
    {
        m_GlobalHook = Hook.GlobalEvents();

        m_GlobalHook.MouseDownExt += GlobalHookMouseDownExt;
        m_GlobalHook.MouseUpExt += GlobalHook_MouseUpExt;
    }

    void GlobalHook_MouseUpExt(object sender, MouseEventExtArgs e)
    {
        if (e.Button == MouseButtons.Left)
        {
            if (m_SupressNextUp)
            {
                Console.WriteLine(@"First mouse up supress.");
                e.Handled = true;
                m_SupressNextDown = true;
            }
            else
            {
                Console.WriteLine(@"Second mouse up - make it heppen.");
                m_SupressNextDown = false;
            }
        }
    }

    private void GlobalHookMouseDownExt(object sender, MouseEventExtArgs e)
    {
        if (e.Button == MouseButtons.Left)
        {
            if (m_SupressNextDown)
            {
                Console.WriteLine(@"Second mouse down supress.");
                e.Handled = true;
                m_SupressNextUp = false;
            }
            else
            {
                Console.WriteLine(@"First mouse down - make it heppen.");
                m_SupressNextUp = true;
            }
        }
    }

    public void Unsubscribe()
    {
        m_GlobalHook.MouseDownExt -= GlobalHookMouseDownExt;
        m_GlobalHook.MouseUpExt -= GlobalHook_MouseUpExt;

        m_GlobalHook.Dispose();
    }
}

这篇关于拦截鼠标点击的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆