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._第1张图片

你可能感兴趣的:(C# monitor keyboard and mouse actions based on MouseKeyHook.)