Windows键盘钩子
Runtime:VS2013
#include "stdafx.h"
#include <windows.h>
#include <conio.h> DWORD g_main_tid = ;
HHOOK g_kb_hook = ; bool CALLBACK con_handler(DWORD CtrlType)
{
PostThreadMessage(g_main_tid, WM_QUIT, , );
return TRUE;
}; LRESULT CALLBACK kb_proc(int code, WPARAM w, LPARAM l)
{
PKBDLLHOOKSTRUCT p = (PKBDLLHOOKSTRUCT)l;
const char *info = NULL;
if (w == WM_KEYDOWN)
info = "key dn";
else if (w == WM_KEYUP)
info = "key up";
else if (w == WM_SYSKEYDOWN)
info = "sys key dn";
else if (w == WM_SYSKEYUP)
info = "sys key up";
printf("%s - vkCode [%04x], scanCode [%04x]\n",
info, p->vkCode, p->scanCode);
// always call next hook
return CallNextHookEx(g_kb_hook, code, w, l);
}; DWORD WINAPI KbHookThread(LPVOID lpParam)
{
g_main_tid = GetCurrentThreadId(); //register Ctrl+C callback
SetConsoleCtrlHandler((PHANDLER_ROUTINE)&con_handler, TRUE);
g_kb_hook = SetWindowsHookEx(WH_KEYBOARD_LL, //Installs a hook procedure that monitors low-level keyboard input events
(HOOKPROC)&kb_proc, //A hook procedure that monitors low-level keyboard input events
NULL,//A handle to the DLL containing the hook procedure pointed to by the lpfn parameter. The hMod parameter must be set to NULL if the dwThreadId parameter specifies a thread created by the current process and if the hook procedure is within the code associated with the current process.(GetModuleHandle(NULL))
);//The identifier of the thread with which the hook procedure is to be associated. For desktop apps, if this parameter is zero, the hook procedure is associated with all existing threads running in the same desktop as the calling thread. For Windows Store apps, see the Remarks section.
if (g_kb_hook == NULL) {
printf("SetWindowsHookEx failed, %ld\n", GetLastError());
return ;
} // Message loop
MSG msg; //If GetMessage retrieves the WM_QUIT message, the return value is zero.
while (GetMessage(&msg, NULL, , )) {
TranslateMessage(&msg);
DispatchMessage(&msg);
} //exit the thread
UnhookWindowsHookEx(g_kb_hook);
return ;
}; int _tmain(int argc, _TCHAR* argv[])
{
HANDLE h = CreateThread(NULL, , KbHookThread, NULL, , NULL);
if (NULL == h) {
printf("CreateThread failed, %ld\n", GetLastError());
return -;
} //wait thread to exit
WaitForMultipleObjects(, &h, TRUE, INFINITE); return ;
}
Windows键盘钩子的更多相关文章
- 安全之路 —— 使用Windows全局钩子打造键盘记录器
		
简介 键盘记录功能一直是木马等恶意软件窥探用户隐私的标配,那么这个功能是怎么实现的呢?在Ring3级下,微软就为我们内置了一个Hook窗口消息的API,也就是SetWindowsHookEx函数,这个 ...
 - windows 键盘全局钩子
		
// HookapiTest.cpp : 定义控制台应用程序的入口点. // #include "stdafx.h" #include <Windows.h> #inc ...
 - 使用windows函数SetWindowsHookEx实现键盘钩子
		
unit Unit1; interface uses Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms ...
 - 使用Windows全局钩子打造键盘记录器
		
简介 键盘记录功能一直是木马等恶意软件窥探用户隐私的标配,那么这个功能是怎么实现的呢?在Ring3级下,微软就为我们内置了一个Hook窗口消息的API,也就是SetWindowsHookEx函数,这个 ...
 - windows消息钩子注册底层机制浅析
		
标 题: [原创]消息钩子注册浅析 作 者: RootSuLe 时 间: 2011-06-18,23:10:34 链 接: http://bbs.pediy.com/showthread.php?t= ...
 - 基于C#实现的HOOK键盘钩子实例代码
		
本文所述为基于C#实现的HOOK实例,该实例可用来屏蔽系统热键.程序主要实现了安装钩子.传递钩子.卸载钩子等功能.在传递钩子中:<param name="pHookHandle&quo ...
 - C#鼠标键盘钩子
		
using System;using System.Collections.Generic; using System.Reflection; using System.Runtime.Interop ...
 - C# 键盘钩子
		
p{ text-align:center; } blockquote > p > span{ text-align:center; font-size: 18px; color: #ff0 ...
 - WPF 利用键盘钩子来捕获键盘,做一些不为人知的事情...完整实例
		
键盘钩子是一种可以监控键盘操作的指令. 看到这句话是不是觉得其实键盘钩子可以做很多事情. 场景 当你的程序需要一个全局的快捷键时,可以考虑使用键盘钩子,如大家常用qq的截图快捷键,那么在WPF里怎么去 ...
 
随机推荐
- linux  date -s
			
修改linux的时间可以使用date指令 修改日期: 时间设定成2009年5月10日的命令如下: #date -s 05/10/2009 修改时间: 将系统时间设定成上午10点18分0秒的命令如下. ...
 - mac-redis安装与使用
			
安装: brew install redis --------------- 使用: 启动redis-server: sudo redis-server 连接:./redis-cli -h 127.0 ...
 - jQuery使用():Deferred有状态的回调列表(含源码)
			
deferred的功能及其使用 deferred的实现原理及模拟源码 一.deferred的功能及其使用 deferred的底层是基于callbacks实现的,建议再熟悉callbacks的内部机制前 ...
 - Python中所有的关键字
			
在python中若想查询python中有哪些关键字可以先导入keyword模块 import keyword #导入关键字模块print(keyword.kwlist) #查询所有关键字 查询结果: ...
 - Anniversary party POJ - 2342 (树形DP)
			
题目链接: POJ - 2342 题目大意:给你n个人,然后每个人的重要性,以及两个人之间的附属关系,当上属选择的时候,他的下属不能选择,只要是两个人不互相冲突即可.然后问你以最高领导为起始点的关系 ...
 - JavaScript定时器详解
			
假设有以下场景 setTimeout(function timeoutHandler(){ /*Some timeout handle code that runs for 6ms*/ }, 10); ...
 - VS2013创建ASP.NET应用程序描述
			
你的 ASP.NET 应用程序 恭喜! 你已创建了一个项目 此应用程序包含: 显示“主页”.“关于”和“联系方式”之间的基本导航的示例页 使用 Bootstrap 进行主题定位 身份验证,如果选择此项 ...
 - 【转】ContextLoaderListener和DispatcherServlet加载内容的区别
			
一.ContextLoaderListener加载内容 二.DispatcherServlet加载内容 ContextLoaderListener和DispatcherServlet都会在Web容器启 ...
 - [JavaScript]ECMA-6 箭头函数
			
概述 箭头函数的作用是为Js提供一种函数的简写方法,箭头函数作用域内不包含this, arguments, super, or new.target,并且不能用于对象的构造函数: 基本语法 [(][p ...
 - String,下表和切片,分割
			
字符串介绍 1.字符串在内存中的存储 2.字符串的加法 3.字符串的格式化 1. 下标索引 所谓“下标”,就是编号,就好比超市中的存储柜的编号,通过这个编号就能找到相应的存储空间 字符串中" ...