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键盘钩子的更多相关文章

  1. 安全之路 —— 使用Windows全局钩子打造键盘记录器

    简介 键盘记录功能一直是木马等恶意软件窥探用户隐私的标配,那么这个功能是怎么实现的呢?在Ring3级下,微软就为我们内置了一个Hook窗口消息的API,也就是SetWindowsHookEx函数,这个 ...

  2. windows 键盘全局钩子

    // HookapiTest.cpp : 定义控制台应用程序的入口点. // #include "stdafx.h" #include <Windows.h> #inc ...

  3. 使用windows函数SetWindowsHookEx实现键盘钩子

    unit Unit1; interface uses Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms ...

  4. 使用Windows全局钩子打造键盘记录器

    简介 键盘记录功能一直是木马等恶意软件窥探用户隐私的标配,那么这个功能是怎么实现的呢?在Ring3级下,微软就为我们内置了一个Hook窗口消息的API,也就是SetWindowsHookEx函数,这个 ...

  5. windows消息钩子注册底层机制浅析

    标 题: [原创]消息钩子注册浅析 作 者: RootSuLe 时 间: 2011-06-18,23:10:34 链 接: http://bbs.pediy.com/showthread.php?t= ...

  6. 基于C#实现的HOOK键盘钩子实例代码

    本文所述为基于C#实现的HOOK实例,该实例可用来屏蔽系统热键.程序主要实现了安装钩子.传递钩子.卸载钩子等功能.在传递钩子中:<param name="pHookHandle&quo ...

  7. C#鼠标键盘钩子

    using System;using System.Collections.Generic; using System.Reflection; using System.Runtime.Interop ...

  8. C# 键盘钩子

    p{ text-align:center; } blockquote > p > span{ text-align:center; font-size: 18px; color: #ff0 ...

  9. WPF 利用键盘钩子来捕获键盘,做一些不为人知的事情...完整实例

    键盘钩子是一种可以监控键盘操作的指令. 看到这句话是不是觉得其实键盘钩子可以做很多事情. 场景 当你的程序需要一个全局的快捷键时,可以考虑使用键盘钩子,如大家常用qq的截图快捷键,那么在WPF里怎么去 ...

随机推荐

  1. Django框架基础之Form组件

    服务端假设所有用户提交的数据都是不可信任的,所以Django框架内置了form组件来验证用户提交的信息 form组件的2大功能:       1 验证(显示错误信息)       2 保留用户上次输入 ...

  2. 什么是MIPI

        随着客户要求手机摄像头像素越来越高同时要求高的传输速度传统的并口传输越来越受到挑战.提高并口传输的输出时钟是一个办法但会导致系统的EMC设计变得越来困难,增加传输线的位数是但是这又不符合小型化 ...

  3. RTC及sensor时间同步

    https://blog.csdn.net/dai_jing/article/details/38147419 ----------------------------- linux 的系统时间有时跟 ...

  4. Python菜鸟快乐游戏编程_pygame(2)

    Python菜鸟快乐游戏编程_pygame(博主录制,2K分辨率,超高清) https://study.163.com/course/courseMain.htm?courseId=100618802 ...

  5. php操作excel表格的导入和导出

    前言:对于excel大家肯定熟悉不过了的,那么我们在日常的业务中应该是有对这些文件的导入导出操作的 类的下载:composer require phpoffice/phpexcel,其中Classes ...

  6. 页面循环绑定(变量污染问题),js面向对象编程(对象属性增删改查),js字符串操作,js数组操作

    页面循环绑定(变量污染问题) var lis = document.querySelectorAll(".ul li") for ( var i = 0 ; i < lis. ...

  7. 在Windows Server上安装ASP.NET时失败,提示not enough storage is available to process the command

    今天在部署ASP.NET网站时出现IIS 500.21错误.环境是Windows Server 2012 +IIS8 于是查找解决方案,发现网上的信息都说是需要重装.NET framerwork4.0 ...

  8. iTOP-i.MX6Q开发板支持安卓Android6.0系统

    迅为IMX6开发板: Android4.4/6.0系统  Linux + Qt5.7系统  Ubuntu12.04系统 部分案例:HMI:3D打印机:医疗设备:工控机:触控一体机:车载终端 核心板兼容 ...

  9. 环境判断:区别h5打开还是weixin打开?

    var source_platform = '' , pf_source = 2; //系统平台 //区别是否是 微信浏览器 , source_platform 系统平台 2018.12.6新增 va ...

  10. C#解压文件,Excel操作

    /// <summary> /// 获取目录下文件路径 /// </summary> /// <param name="path"></p ...