-2

I know c# and i want create a fuction for deactive and active mouse left/right button only (in c# code) like a AHK program. Can you help me?

Greenjack
  • 13
  • 2

1 Answers1

0

Have a look at https://globalmousekeyhook.codeplex.com/, it has complete facilities for global keyboard and mouse hook.

the example below is based on mentioned library for mouse right click suppression

Right Click Suppressor

using System;
using System.Windows.Forms;
using MouseKeyboardActivityMonitor.WinApi;

namespace Demo
{
    class MouseRightClickDisable:IDisposable
    {
        private readonly MouseHookListener _mouseHook;

        public MouseRightClickDisable()
        {
            _mouseHook=new MouseHookListener(new GlobalHooker());
            _mouseHook.MouseDownExt += MouseDownExt;
            _mouseHook.Enabled = true;
        }

        private void MouseDownExt(object sender, MouseEventExtArgs e)
        {
            if (e.Button != MouseButtons.Right) { return; }
            e.Handled = true; //suppressing right click
        }


        public void Dispose()
        {
            _mouseHook.Enabled = false;
            _mouseHook.Dispose();
        }
    }
}

Usage

internal static class Program
{

    [STAThread]
    private static void Main()
    {
        using (new MouseRightClickDisable())
            Application.Run();
    }
}

Console Sample

NOTE: add System.Windows.Forms.dll as reference to your console application

internal static class Program
{
    [STAThread]
    private static void Main()
    {
        Console.WriteLine("Mouse Hook");

        // our UI thread here
        var t = new Thread(() =>
        {
            using (new MouseRightClickDisable())
                Application.Run();
        });

        t.Start();

        //some console task here!
        for (var i = 0; i < 10; i++)
        {
            Console.WriteLine("Console Task {0}",i);
            Thread.Sleep(100);
        }


        Console.WriteLine("press any key to terminate the application...");
        Console.ReadKey();
        Application.Exit();
    }
}
user3473830
  • 7,165
  • 5
  • 36
  • 52
  • i can use it in console application? – Greenjack Oct 09 '14 at 20:07
  • technically yes, your program can be a `Console Application`, but you need `Application.Run()` to do UI message pumping or you should implement your own UI message loop. I updated the answer accordingly. – user3473830 Oct 09 '14 at 20:33
  • Ok fine but i don't understand how disable or re-activate right button mouse. Who is the fuction? – Greenjack Oct 09 '14 at 22:25
  • its in the `private void MouseDownExt(object sender, MouseEventExtArgs e)` method. `if (e.Button != MouseButtons.Right)` checks whether pushed button is right click or not and `e.Handled = true; //suppressing right click` disables the key (if you set `Handled` to true it will re-enable it). – user3473830 Oct 10 '14 at 03:03