首页 > Windows开发 > 详细

C# monitor keyboard and mouse actions based on MouseKeyHook.

时间:2020-01-15 17:58:35      阅读:89      评论:0      收藏:0      [点我收藏+]

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.

原文:https://www.cnblogs.com/Fred1987/p/12197851.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!