⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 keyhookapp.cpp

📁 《windows程序设计》王艳平版的书籍源代码
💻 CPP
字号:
////////////////////////////////////////////////
// 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;
}


⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -