第九章 keyhook windows程序设计王艳平版

///
// MainDir.h文件 // 07MainDir


#include <afxwin.h>




class CMyApp : public CWinApp
{
public:
BOOL InitInstance();
};


class CMainDialog : public CDialog
{
public:
CMainDialog(CWnd* pParentWnd = NULL);


protected:
virtual BOOL OnInitDialog();
virtual void OnCancel();


afx_msg long OnHookKey(WPARAM wParam, LPARAM lParam);


DECLARE_MESSAGE_MAP()

};



// KeyHookApp.cpp文件


#include "resource.h"
#include "KeyHookApp.h"
#include "../09KeyHookLib/KeyHookLib.h"


#pragma comment(lib, "09KeyHookLib")


CMyApp theApp;


BOOL CMyApp::InitInstance()
{
CMainDialog dlg;
m_pMainWnd = &dlg;
dlg.DoModal();
return FALSE;
}


CMainDialog::CMainDialog(CWnd* pParentWnd):CDialog(IDD_MAIN, pParentWnd)
{
}


BEGIN_MESSAGE_MAP(CMainDialog, CDialog)
ON_MESSAGE(HM_KEY, OnHookKey)
END_MESSAGE_MAP()



BOOL CMainDialog::OnInitDialog()
{
CDialog::OnInitDialog();
SetIcon(theApp.LoadIcon(IDI_MAIN), FALSE);
::SetWindowPos(m_hWnd, HWND_TOPMOST, 0, 0, 
0, 0, SWP_NOSIZE|SWP_NOREDRAW|SWP_NOMOVE);


// 安装钩子
if(!SetKeyHook(TRUE, 0, m_hWnd))
MessageBox("安装钩子失败!");


return TRUE;
}




void CMainDialog::OnCancel()
{
// 卸载钩子
SetKeyHook(FALSE);
CDialog::OnCancel();
return;
}


long CMainDialog::OnHookKey(WPARAM wParam, LPARAM lParam)
{
// 此时参数wParam为用户按键的虚拟键码,
// lParam参数包含按键的重复次数、扫描码、前一个按键状态等信息


char szKey[80];
::GetKeyNameText(lParam, szKey, 80);


CString strItem;
strItem.Format(" 用户按键:%s \r\n", szKey);
// 添加到编辑框中
CString strEdit;
GetDlgItem(IDC_KEYMSG)->GetWindowText(strEdit);
GetDlgItem(IDC_KEYMSG)->SetWindowText(strItem + strEdit);


::MessageBeep(MB_OK);
return 0;
}




  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值