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. jumpserver win终端无法添加

    今天遇到一个问题是,guacd,tomcat部署完成以后jumpserver终端管理页里面还是只有coco的终端而没有guacd的终端

  2. kms访问数据库的方式(该篇只是作为个人笔记,不具有任何公共参考意图)

    项目类型:winform 语言:C# 服务程序:webservice(webservice我本人也不了解,在下一章中会总结一下对它的概念的简单理解) 情景描述:简单创建一个窗体,实现学生信息(姓名.性 ...

  3. android studio 报红解决

    /Users/houzhibin/gradle/wrapper/dists/gradle-4.1-all/bzyivzo6n839fup2jbap0tjew/gradle-4.1-all.zip

  4. Angular记录(10)

    文档资料 速查表:https://www.angular.cn/guide/cheatsheet 风格指南:https://www.angular.cn/guide/styleguide Angula ...

  5. linux路由表

    Linux 内核的路由表通过 route 命令查看 Linux 内核的路由表: [root@VM_139_74_centos ~]# routeKernel IP routing tableDesti ...

  6. 【转载】C++ STL快速入门

    https://www.cnblogs.com/skyfsm/p/6934246.html

  7. [算法竞赛入门经典] 象棋 ACM/ICPC Fuzhou 2011, UVa1589 较详细注释

    Description: Xiangqi is one of the most popular two-player board games in China. The game represents ...

  8. MySQL sum聚合函数

    select sum(if(money > 0, money, 0)) as money   from total_money 意思是如果money > 0, 将money的值累加到tot ...

  9. luogu P5320 [BJOI2019]勘破神机

    传送门 首先我们要知道要求什么.显然每次放方块要放一大段不能从中间分开的部分.设\(m=2\)方案为\(f\),\(m=3\)方案为\(g\),\(m=2\)可以放一个竖的,或者两个横的,所以\(f_ ...

  10. mysql8.0卸载干净--win10

    本文介绍,在Windows10系统下,如何彻底删除卸载MySQL... 1>停止MySQL服务开始->所有应用->Windows管理工具->服务,将MySQL服务停止.2> ...