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; } = ;
public static int DoubleClickCount { get; set; } = ;
public static int WheelCount { get; set; } = ;
public static int MoveCount { get; set; } = ;
public static int PressCount { get; set; } = ;
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();
 

C# monitor keyboard and mouse actions based on MouseKeyHook.的更多相关文章

  1. C# monitor keyboard and print pressed key

    using System; using System.Windows.Forms; using System.Runtime.InteropServices; using System.Diagnos ...

  2. VNC ( Virtual Network Computing )

    VNC is used to display an X windows session running on another computer. Unlike a remote X connectio ...

  3. run a Freight robot (2)

    3.  Network Setup Connecting Freight to a Monitor The easiest way to configure the wireless networki ...

  4. windows API 第22篇 WTSGetActiveConsoleSessionId

    函数原型:DWORD WTSGetActiveConsoleSessionId (VOID)先看一下原文介绍: The WTSGetActiveConsoleSessionId function re ...

  5. jetson nano开发使用的基础详细分享

    前言: 最近拿到一块jetson nano 2GB版本的板子,折腾了一下,从烧录镜像.修改配件等,准备一篇开箱基础文章给大家介绍一下这块AI开发板. 作者:良知犹存 转载授权以及围观:欢迎关注微信公众 ...

  6. PatentTips - Scheduling compute kernel workgroups to heterogeneous processors based on historical processor execution times and utilizations

    BACKGROUND OF THE INVENTION  1. Field of the Invention  The present invention relates generally to h ...

  7. Javascript Madness: Mouse Events

    http://unixpapa.com/js/mouse.html Javascript Madness: Mouse Events Jan WolterAug 12, 2011 Note: I ha ...

  8. java 并发官方教程

    http://docs.oracle.com/javase/tutorial/essential/concurrency/index.html Concurrency Computer users t ...

  9. usb.ids

    # # List of USB ID's # # Maintained by Vojtech Pavlik <vojtech@suse.cz> # If you have any new ...

随机推荐

  1. 使用visual studio 2013读取.mat文件

    现在有一个T.mat 文件需要在c++中处理然后以.mat 或是.txt形式返回 T.mat中存储了十个cell,每个cell中会有一个不等长的数组 1.以下是相关配置过程: 参考:http://we ...

  2. Gloang Swagger

    功能 自动化生产接口文档 安装 # 安装swaggo get -u github.com/swaggo/swag/cmd/swag # 安装 gin-swagger go get -u github. ...

  3. 【阿里云IoT+YF3300】13.阿里云IoT Studio WEB监控界面构建

    Web可视化开发是阿里云IoT Studio中比较重要的一个功能,通过可视化拖拽的方式,方便地将各种图表组件与设备相关的数据源关联,无需编程,即可将物联网平台上接入的设备数据可视化展现. 目前支持的组 ...

  4. 实现一个简易的RPC

    之前写了一些关于RPC原理的文章,但是觉得还得要实现一个.之前看到一句话觉得非常有道理,与大家共勉.不是“不要重复造轮子”,而是“不要发明轮子”,所以能造轮子还是需要造的. 如果大家还有不了解原理的, ...

  5. WTL Picture Control显示图片

    1.在对话框上新建一个Picture Control ID为:IDC_STATIC_IMG 2.添加图片资源ID:IDB_BITMAP1(选中Bitmap点击导入,选择bmp图片资源) 实现: CWi ...

  6. QT5如何设置QLabel中字体的颜色

    修改了wd的文章: 如何使用Qt5,设置QLabel中字体的颜色. 大致有几种做法: 一是使用setPalette()方法: 二是使用样式表: 三是可以使用QStyle: 四是可以在其中使用一些简单的 ...

  7. python3调用哈工大ltp

    运行环境ubuntu+python3 安装pyltp sudo pip3 install pyltp 下载ltp_data_v3.4.0模型 http://ltp.ai/ 分句 from pyltp ...

  8. css- :before :after

    :before和:after的作用就是在指定的元素内容(而不是元素本身)之前或者之后插入一个包含content属性指定内容的行内元素,最基本的用法如下: #example:before { conte ...

  9. 曹工说Spring Boot源码(17)-- Spring从xml文件里到底得到了什么(aop:config完整解析【中】)

    写在前面的话 相关背景及资源: 曹工说Spring Boot源码(1)-- Bean Definition到底是什么,附spring思维导图分享 曹工说Spring Boot源码(2)-- Bean ...

  10. Nginx 部署及配置

    Tengine + Luajit2 系统账号及环境配置 $ sudo useradd -g 100 -u 200 user_00 $ sudo groupadd -g 300 www $ sudo u ...