Intercept Mouse Click

廉价感情. 提交于 2019-12-14 02:15:48

问题


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.

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.

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?

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();

            }
        }
    }

回答1:


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.

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

How it works?

  1. First mouse down set the flag m_SupressNextUp - down is fired.
  2. Mouse button up which follows previous down is supressed and set the flag m_SupressNextDown
  3. Second down is supressed too and reset the flag m_SupressNextUp
  4. Second up is fired and reset m_SupressNextDown
  5. System is returned to initial state.

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();
    }
}



回答2:


after reviewing the documentation for the input simulator I am guessing you need to reverse the mouse down input.

the source code for the input simulator has a mouse up method

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

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

        }
    }
}


来源:https://stackoverflow.com/questions/30061813/intercept-mouse-click

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!