zoukankan
html css js c++ java
C#.Net的全局键盘钩子(Hook)技术
using
System;
using
System.Windows.Forms;
using
System.Runtime.InteropServices;
using
System.Reflection;
namespace
HookDll
{
public
class
HookDll
{
private
KBDLLHOOKSTRUCT kbdllhs;
private
IntPtr iHookHandle
=
IntPtr.Zero;
private
GCHandle _hookProcHandle;
public
delegate
IntPtr HookProc(
int
nCode, IntPtr wParam, IntPtr lParam);
[DllImport(
"
user32.dll
"
, CharSet
=
CharSet.Auto)]
public
static
extern
IntPtr SetWindowsHookEx(
int
hookid, HookProc pfnhook, IntPtr hinst,
int
threadid);
[DllImport(
"
user32.dll
"
, CharSet
=
CharSet.Auto, ExactSpelling
=
true
)]
public
static
extern
bool
UnhookWindowsHookEx(IntPtr hhook);
[DllImport(
"
user32.dll
"
, CharSet
=
CharSet.Auto, ExactSpelling
=
true
)]
public
static
extern
IntPtr CallNextHookEx(IntPtr hhook,
int
code, IntPtr wparam, IntPtr lparam);
[DllImport(
"
kernel32.dll
"
,CharSet
=
CharSet.Auto, CallingConvention
=
CallingConvention.StdCall)]
private
static
extern
IntPtr GetModuleHandle(
string
lpModuleName);
[DllImport(
"
Kernel32.dll
"
,EntryPoint
=
"
RtlMoveMemory
"
)]
public
static
extern
void
CopyMemory (
ref
KBDLLHOOKSTRUCT Source,IntPtr Destination,
int
Length);
private
const
int
WH_KEYBOARD
=
13
;
public
void
DisableKBDHook()
{
try
{
if
(iHookHandle
!=
IntPtr.Zero)
{
UnhookWindowsHookEx(iHookHandle);
}
_hookProcHandle.Free();
iHookHandle
=
IntPtr.Zero;
}
catch
{
return
;
}
}
public
void
EnableKBDHook()
{
HookProc hookProc
=
new
HookProc(KBDDelegate);
_hookProcHandle
=
GCHandle.Alloc(hookProc);
iHookHandle
=
SetWindowsHookEx(WH_KEYBOARD, hookProc, GetModuleHandle(
"
HookDll.dll
"
),
0
);
if
(iHookHandle
==
IntPtr.Zero)
{
throw
new
System.Exception(
"
错误,钩子失败!
"
);
}
}
public
IntPtr KBDDelegate(
int
iCode , IntPtr wParam,IntPtr lParam)
{
kbdllhs
=
new
KBDLLHOOKSTRUCT();
CopyMemory(
ref
kbdllhs, lParam,
20
);
//
结果就在这里了^_^
int
iHookCode
=
kbdllhs.vkCode;
DisableKBDHook();
EnableKBDHook();
return
CallNextHookEx(iHookHandle, iCode, wParam,lParam);
}
}
[StructLayout(LayoutKind.Sequential)]
public
struct
KBDLLHOOKSTRUCT
{
public
int
vkCode;
public
int
scanCode;
public
int
flags;
public
int
time;
public
int
dwExtraInfo;
}
}
查看全文
相关阅读:
口罩预约管理系统——系统网站实现(前端+PHP+MySQL)
口罩预约管理系统——数据库设计(前端+PHP+MySQL)
购书网站前端实现(HTML+CSS+JavaScript)
HMM隐马尔可夫模型来龙去脉(二)
Linux环境编程进程间通信机制理解
Linux基本命令及编程环境实验
大数据技术
ModuleNotFoundError: No module named '_ctypes'的解决方案
page-break-inside,page-break-before 分页
C.char字符串的拼接和const char*的转换
原文地址:https://www.cnblogs.com/MaxWoods/p/1205221.html
最新文章
[转]JAVA 线上故障排查套路,从 CPU、磁盘、内存、网络到GC 一条龙!
[转]设计模式在工作中的实践
[转]GET和POST两种基本请求方法有什么区别
[转]应对缓存穿透
[转]如何判断字典a在字典b 接口测试验证点
[转]JAVA 线上故障排查套路,从 CPU、磁盘、内存、网络到GC
[转]Springboot接口简单实现生成MySQL插入语句
[转]性能工具之Jmeter通过springboot工程启动
[转]【秒杀系统架构】分析与实战
[转]徒手撸自动化平台
热门文章
php 23种设计模式
php 23种设计模型
php 23种设计模式
php 23种设计模式
Linux 系统目录结构
TCP三次握手和四次挥手以及11种状态
验证码实现原理
UDP协议网络Socket编程(java实现C/S通信案例)
CISCO交换机STP实验(生成树协议)
双向最大匹配算法——基于词典规则的中文分词(Java实现)
Copyright © 2011-2022 走看看