// keyboardhook.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include<iostream>
#include<windows.h>
using namespace std;
HHOOK g_Hook;
LRESULT CALLBACK LowLevelKeyboardProc(INT nCode, WPARAM wParam, LPARAM lParam)
{
KBDLLHOOKSTRUCT *pkbhs = (KBDLLHOOKSTRUCT *)lParam;
BOOL bControlKeyDown = 0;
switch (nCode)
{
case HC_ACTION:
{
// Check to see if the CTRL key is pressed
bControlKeyDown = GetAsyncKeyState (VK_CONTROL) >> ((sizeof(SHORT) * 8) - 1);
//Check to see if the Cap is pressed
BOOL bCap = GetAsyncKeyState(VK_CAPITAL) >> ((sizeof(SHORT) * 8) - 1);
//Disable CTRL+ESC
if (pkbhs->vkCode == VK_ESCAPE && bControlKeyDown)
return 1;
if(wParam == WM_KEYUP)
printf("%c", pkbhs->vkCode);
break;
}
}
//Passes the hook information to the next hook procedure in the current hook chain. A hook procedure can call this function either before or after processing the hook information.
return CallNextHookEx(g_Hook, nCode, wParam, lParam); //回调
//return 1;
}
int _tmain(int argc, _TCHAR* argv[])
{
MSG msg;
//Installs an application-defined hook procedure into a hook chain. You would install a hook procedure to monitor the system for certain types of events. These events are associated either with a specific thread or with all threads in the same desktop as the calling thread.
g_Hook=(HHOOK)SetWindowsHookEx(WH_KEYBOARD_LL,
(HOOKPROC)LowLevelKeyboardProc, GetModuleHandleW(0),0);
while(GetMessageW(&msg,0,0,0))DispatchMessageW(&msg);
return 0;
}