Monitor multi-Process keyboard operation with keyboard global hook hook

Source: Internet
Author: User
Tags bool exit

Free to do nothing, under the Win2K with BCB5 do a keyboard hook applet, monitor the global key situation. The hook placement and callback functions are placed in a separate DLL, and the DLL's original code is as follows:

//----------------------------------------------------------------------------------------------------
extern "C" __declspec (dllexport) void __stdcall sethook (hwnd,bool);
LRESULT CALLBACK hookproc (int ncode,wparam wparam,lparam LPARAM)
//----------------------------------------------------------------------------------------------------
Static HINSTANCE hinstance; Apply instance Handle
Static HWND Hwndmain; MainForm Handle
Static Hhook Hkeyhook; Hook handle
static const mymessage=2000; Custom message number
static const secondpar=1; Custom message 2nd parameter
//----------------------------------------------------------------------------------------------------
int WINAPI DllEntryPoint (hinstance hinst, unsigned long reason, void* lpreserved)
{hinstance=hinst; return 1;}
//----------------------------------------------------------------------------------------------------
void __stdcall Sethook (HWND hmainwin,bool ncode)
{
if (ncode)//placement Hook
{
Hwndmain=hmainwin;
Hkeyhook=setwindowshookex (Wh_journalrecord, (HookProc) hookproc,hinstance,0);
}
else//Remove hook
UnhookWindowsHookEx (Hkeyhook);
}
//----------------------------------------------------------------------------------------------------
LRESULT CALLBACK hookproc (int ncode,wparam wparam,lparam LPARAM)
{
Eventmsg *keymsg= (eventmsg *) LParam;
if ((ncode==hc_action) && (Keymsg->message==wm_keyup)
PostMessage (Hwndmain,mymessage, (char) (KEYMSG->PARAML), secondpar);
Send a message to the calling form Mymessage and virtual key Code (char) (KEYMSG->PARAML)
return ((int) CallNextHookEx (hkeyhook,ncode,wparam,lparam));
}
//----------------------------------------------------------------------------------------------------
The application code is as follows: (Tuning DLL)
//----------------------------------------------------------------------------------------------------
Static HINSTANCE hDLL; DLL handle
typedef void __stdcall (*dllfun) (Hwnd,bool);
Dllfun Dllsethook;
static const mymessage=2000;
static const secondpar=1;
//----------------------------------------------------------------------------------------------------
__fastcall Tform1::tform1 (tcomponent* owner): Tform (owner)
{}
//----------------------------------------------------------------------------------------------------
void __fastcall tform1::formcreate (tobject *sender)
{
Hdll=loadlibrary ((LPCTSTR) "Project1.dll"); DLL file name: Project1.dll
if (hdll==null)
{ShowMessage ("DLL: Cannot load!) Program exits. "); Exit (1); }
Dllsethook = (dllfun) GetProcAddress (hDLL, "Sethook");
if (dllsethook==null)
{ShowMessage ("DLL: Function not found!) Program exits. "); FreeLibrary (hDLL); Exit (1); }
Dllsethook (this->handle,true);
}
//----------------------------------------------------------------------------------------------------
void __fastcall Tform1::formclose (tobject *sender, tcloseaction &action)
{
Dllsethook (Null,false); Remove Hook
FreeLibrary (hDLL); Remove DLL
}
//----------------------------------------------------------------------------------------------------
void __fastcall tform1::applicationevents1message (tagmsg &msg,bool &handled)
{//BCB5.0 ApplicationEvents components
if ((msg.message==mymessage) && (Msg.lparam==secondpar)
ShowMessage ("Receive the hook button message!") \ n \ nthe "key virtual code": "+inttostr (Msg.wparam));
}
//----------------------------------------------------------------------------------------------------

Hook with Wh_journalrecord type is simple to implement.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.