C# monitor keyboard and mouse actions based on MouseKeyHook.

1.Install-package MouseKeyHook

2.

using Gma.System.MouseKeyHook;
using System; 

namespace ConsoleApp1
{
    public class MonitorHelper
    {
        public static void ListenForMouseEvents()
        {
            Hook.GlobalEvents().MouseClick += (sender, e) =>
            {
                Console.WriteLine($"{DateTime.Now.ToString("yyyyMMddHHmmssffff")} Mouse {e.Button} clicked.");
            };

            Hook.GlobalEvents().MouseDoubleClick += (sender, e) =>
           {
               Console.WriteLine($"{DateTime.Now.ToString("yyyyMMddHHmmssffff")} Mouse {e.Button} button double clicked.");
           };

            Hook.GlobalEvents().MouseDragFinished += (sender, e) =>
            {
                Console.WriteLine($"{DateTime.Now.ToString("yyyyMMddHHmmssffff")} Mouse {e.Button} dragged");
            };

            Hook.GlobalEvents().MouseWheel += (sender, e) =>
            {
                Console.WriteLine($"{DateTime.Now.ToString("yyyyMMddHHmmssffff")} Mouse scrolls");
            };

            Hook.GlobalEvents().KeyDown += (sender, e) =>
            {
                Console.WriteLine($"{DateTime.Now.ToString("yyyyMMddHHmmssffff")} pressed {e.KeyCode}");
            };
        }
    }
}

3.

static void Main(string[] args)
        {
            MouseMonitor();
            Console.ReadLine();
        }

        static void MouseMonitor()
        {
            MonitorHelper.ListenForMouseEvents();
            Application.Run(new ApplicationContext());
        }

 

C# monitor keyboard and mouse actions based on MouseKeyHook.

C# monitor keyboard and mouse actions based on MouseKeyHook.

上一篇:Git for Windows的使用(含Intellij idea)


下一篇:C#查看文件目录操作、复制、替换