欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

C# monitor keyboard and mouse actions based on MouseKeyHook.

程序员文章站 2022-07-04 22:33:14
1.Install-package MouseKeyHook 2. using Gma.System.MouseKeyHook; using System; namespace ConsoleApp1 { public class MonitorHelper { public static void ......

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.