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; } = ;
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.的更多相关文章
- C# monitor keyboard and print pressed key
using System; using System.Windows.Forms; using System.Runtime.InteropServices; using System.Diagnos ...
- VNC ( Virtual Network Computing )
VNC is used to display an X windows session running on another computer. Unlike a remote X connectio ...
- run a Freight robot (2)
3. Network Setup Connecting Freight to a Monitor The easiest way to configure the wireless networki ...
- windows API 第22篇 WTSGetActiveConsoleSessionId
函数原型:DWORD WTSGetActiveConsoleSessionId (VOID)先看一下原文介绍: The WTSGetActiveConsoleSessionId function re ...
- jetson nano开发使用的基础详细分享
前言: 最近拿到一块jetson nano 2GB版本的板子,折腾了一下,从烧录镜像.修改配件等,准备一篇开箱基础文章给大家介绍一下这块AI开发板. 作者:良知犹存 转载授权以及围观:欢迎关注微信公众 ...
- 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 ...
- Javascript Madness: Mouse Events
http://unixpapa.com/js/mouse.html Javascript Madness: Mouse Events Jan WolterAug 12, 2011 Note: I ha ...
- java 并发官方教程
http://docs.oracle.com/javase/tutorial/essential/concurrency/index.html Concurrency Computer users t ...
- usb.ids
# # List of USB ID's # # Maintained by Vojtech Pavlik <vojtech@suse.cz> # If you have any new ...
随机推荐
- 【转】spring framework 5以前体系结构及内部各模块jar之间的maven依赖关系
作者:凌承一 出处:http://www.cnblogs.com/ywlaker/ 很多人都在用spring开发java项目,但是配置maven依赖的时候并不能明确要配置哪些spring的jar, ...
- Lighthouse
北大程郁缀教授: 一,"日月之行,若出其中,星汉灿烂,若出其里."要找机会去感受大海,男人要有大海一样的胸怀,大气者方能成大器. 二,"亦余心之所善兮,虽九死其而未悔.& ...
- Huffman编码和解码
一.Huffman树 定义: 给定n个权值作为n个叶子结点,构造一棵二叉树,若该树的带权路径达到最小,这样的二叉树称为最优二叉树,也称为霍夫曼树(Huffman树). 特点: Huffman树 ...
- pandas 使用总结
import pandas as pd import numpy as np ## 从字典初始化df ipl_data = {'Team': ['Riders', 'Riders', 'Devils' ...
- POJ_1564_dfs
题目描述: 每组数据给定一个大的数,和一系列降序的数值,要求列出不重复的数值中挑选的数的和为大数的方案,每一种方案里,每个数值最多只能使用一次. 思路: dfs基础题,每次记录大数和当前总和的差值,当 ...
- ARTS Week 11
Jan 6, 2020 ~ Jan 12, 2020 Algorithm Problem 108 Convert Sorted Array to Binary Search Tree (将有序数组转化 ...
- java9String类简单了解
public class jh_01_String类简单了解 { public static void main(String[] args) { /* * 函数:完成特定功能的代码块. * next ...
- 搭建python运行环境
一.下载Anaconda Anaconda是Python的包管理器和环境管理器 https://mirrors.tuna.tsinghua.edu.cn/anaconda/archive/ 参考:ht ...
- css 关于自适应页面
//不能使用绝对宽度的布局 不能使用具有绝对宽度的元素 media_type 设备类型说明 all 所有设备 aural 听觉设备 braille 点字触觉设备 handled 便携设备,如手机.平板 ...
- Day6前端学习之路——布局
一.定位 1)静态定位 position:static(默认) 2)相对定位 position:relative(要配合top.bottom.left.right等属性来使用) 3)绝对定位 pos ...