Click here to Skip to main content
15,884,629 members
Articles / Programming Languages / C++

KeyBoard Hooks

Rate me:
Please Sign up or sign in to vote.
4.76/5 (40 votes)
23 Jul 20012 min read 847.3K   20.4K   163  
This example shows how to write global hooks .This program captures all the Keyboard events and save the keys to a text file.
// installhookDlg.cpp : implementation file
//

#include "stdafx.h"
#include "installhook.h"
#include "installhookDlg.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////
// CInstallhookDlg dialog

CInstallhookDlg::CInstallhookDlg(CWnd* pParent /*=NULL*/)
	: CDialog(CInstallhookDlg::IDD, pParent)
{
	//{{AFX_DATA_INIT(CInstallhookDlg)
		// NOTE: the ClassWizard will add member initialization here
	//}}AFX_DATA_INIT
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}

void CInstallhookDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CInstallhookDlg)
		// NOTE: the ClassWizard will add DDX and DDV calls here
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CInstallhookDlg, CDialog)
	//{{AFX_MSG_MAP(CInstallhookDlg)
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	ON_BN_CLICKED(IDOK, OnOk)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CInstallhookDlg message handlers

BOOL CInstallhookDlg::OnInitDialog()
{
	CDialog::OnInitDialog();

	SetIcon(m_hIcon, TRUE);			// Set big icon
	SetIcon(m_hIcon, FALSE);		// Set small icon
	
	// TODO: Add extra initialization here
	
	return TRUE;  // return TRUE  unless you set the focus to a control
}

// If you add a minimize button to your dialog, you will need the code below
//  to draw the icon.  For MFC applications using the document/view model,
//  this is automatically done for you by the framework.

void CInstallhookDlg::OnPaint() 
{
	if (IsIconic())
	{
		CPaintDC dc(this); // device context for painting

		SendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0);

		// Center icon in client rectangle
		int cxIcon = GetSystemMetrics(SM_CXICON);
		int cyIcon = GetSystemMetrics(SM_CYICON);
		CRect rect;
		GetClientRect(&rect);
		int x = (rect.Width() - cxIcon + 1) / 2;
		int y = (rect.Height() - cyIcon + 1) / 2;

		// Draw the icon
		dc.DrawIcon(x, y, m_hIcon);
	}
	else
	{
		CDialog::OnPaint();
	}
}

HCURSOR CInstallhookDlg::OnQueryDragIcon()
{
	return (HCURSOR) m_hIcon;
}

void CInstallhookDlg::OnOk() 
{
static HINSTANCE hinstDLL; 
typedef BOOL (CALLBACK *inshook)(); 
inshook instkbhook;
hinstDLL = LoadLibrary((LPCTSTR) "hodll.dll"); 
instkbhook = (inshook)GetProcAddress(hinstDLL, "installhook"); 
instkbhook();
ShowWindow(SW_MINIMIZE);
	
}

void CInstallhookDlg::OnCancel() 
{

CDialog::OnCancel();

}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
United States United States
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions