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. ubuntu-14.04.6配置IP

    配置环境与要求: 网卡列表如下: eth0:DHCP模式 eth1:静态模式 网络概况与要求: 192.168.2.0/24为外网(获取网络资源) 10.5.1.0/24为内网(终端服务管理) 系统默 ...

  2. t-io Java构建p2p网络

    Java 构建p2p网络 这篇文章是一篇关于pbft算法实现的一篇补充文章,但是在这里不会涉及pbft的算法方面,所以可以当做一篇单独的文章食用.如果想查看关于区块链或者PBFT算法的文章,可以参考一 ...

  3. LUA提取免费迅雷账号

    --获取http://www.521xunlei.com/ 免费迅雷账号 function getPageid() local http = require("socket.http&quo ...

  4. 位运算在状态压缩DP中的应用

    一.判断一个数字X的i位是不是1 方法:   << (i-)) & x > )  原理: 1左移(i-1)位,相当于制造了一个就i位上是1其他位都是0的一个二进制数.将这个数 ...

  5. SpingBoot错误信息处理及原理

    SpringBoot错误信息处理机制 在一个web项目中,总需要对一些错误进行界面或者json数据返回,已实现更好的用户体验,SpringBoot中提供了对于错误处理的自动配置 ErrorMvcAut ...

  6. 二维数组转稀疏数组、稀疏数组恢复二维数组(Java实现)

    public static void main(String[] args) { // 创建一个原始的二维数组 9*9 int chessArr1[][] = new int[9][9]; // 0表 ...

  7. php单例模式的实现

    <?php /** * 设计模式之单例模式 * $_instance必须声明为静态的私有变量 * 构造函数和析构函数必须声明为私有,防止外部程序new * 类从而失去单例模式的意义 * getI ...

  8. 使用docker搭建FastDFS

    拉取镜像(使用docker-componse可以忽略) [root@localhost ~]# docker pull phinexdaz/fdfs_tracker [root@localhost ~ ...

  9. tomcat增加内存 JVM内存调优

    tomcat总是卡死,查看日志catalina.out 发现疯狂报错 如下,提示内存溢出 java.lang.OutOfMemoryError: Java heap space 此外常见的内存溢出有以 ...

  10. 维基逃离MySQL 力挺开源数据库 MariaDB

    近日全球著名百科类网站维基百科宣布,将不会再用MySQL数据库,据国外媒体报道,很多年,MySQL一直是热门的开源数据库,不过在被甲骨文收购后,面临闭源的风险.因此维基百科将切换到另外一款开源数据库M ...