zoukankan      html  css  js  c++  java
  • C# monitor keyboard and mouse actions based on MouseKeyHook.

    Below is applicable for ConsoleApplication

    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());
            }

    While in desktop application,such as WindowsForm.Please ignore above part and reference below.

     public class MonitorHelper
        {
            public static int ClickCount { get; set; } = 0;
            public static int DoubleClickCount { get; set; } = 0;
            public static int WheelCount { get; set; } = 0;
            public static int MoveCount { get; set; } = 0;
            public static int PressCount { get; set; } = 0;
            private static IKeyboardMouseEvents kmEvents;
            public static void ListenForMouseEvents()
            {
                kmEvents = Hook.GlobalEvents();
                kmEvents.MouseClick += MonitorHelperMouseClick;           
                kmEvents.MouseDoubleClick += MonitorHelperMouseDoubleClick;
                kmEvents.MouseDragFinished += MonitorHelperMouseDragFinished;
                kmEvents.MouseWheel += MonitorHelperMouseWheel;
                kmEvents.KeyDown += MonitorHelperKeyDown;            
            }
    
            private static void MonitorHelperKeyDown(object sender, KeyEventArgs e)
            {
                PressCount++;
            }
    
            private static void MonitorHelperMouseWheel(object sender, MouseEventArgs e)
            {
                WheelCount++;
            }
    
            private static void MonitorHelperMouseDragFinished(object sender, MouseEventArgs e)
            {
                MoveCount++;
            }
    
            private static void MonitorHelperMouseDoubleClick(object sender, MouseEventArgs e)
            {
                DoubleClickCount++;
            }
    
            private static void MonitorHelperMouseClick(object sender, MouseEventArgs e)
            {
                ClickCount++;
            }
    
            public static void MouseMonitor()
            {
                MonitorHelper.ListenForMouseEvents(); 
            }
        }

    There is a big problem in Windows Form when use the first part.It will report exception and bug like below.

      **CallbackOnCollectedDelegate was detected**
    
    A callback was made on a garbage collected delegate of type 'Browser!Utilities.globalKeyboardHook+keyboardHookProc::Invoke'. This may cause application crashes, corruption and data loss. When passing delegates to unmanaged code, they must be kept alive by the managed application until it is guaranteed that they will never be called.

    So we need to declare a new  variable and assign values to it.Then register events based on the new variable instead of the Hook.GlobalEvents.

     
    private static IKeyboardMouseEvents kmEvents; 
    kmEvents = Hook.GlobalEvents();
     
  • 相关阅读:
    ylbtech-dbs-m-QQ邮箱
    ylbtech-Bill(发票管理)-数据库设计
    ylbtech-Recode(记录)-数据库设计
    ylbtech-LanguageSamples-Yield
    ylbtech-LanguageSamples-XMLdoc
    ylbtech-LanguageSamples-Versioning(版本控制)
    线程局部变量的使用
    守护线程的创建和运行
    有助于改善性能的技巧
    使用NIO提升性能
  • 原文地址:https://www.cnblogs.com/treeskyer/p/12749557.html
Copyright © 2011-2022 走看看