当应用程序不在焦点时,如何使应用程序检测到 Kay press?[已解决] 当应用程序窗口没有聚焦时,我需要在插入键按下时启动计时器并在再次按下时停止。 谁能告诉我源代码或一些基于 MFC 的示例?我知道 MFC 没有那种成员,但是在 MFC 中实现的正确源代码看起来如何? 如何通过按键启动计时器?
// MainHamsterDlg.cpp : implementation file
#include "stdafx.h"
#include "MainHamsterDlg.h"
// MainHamsterDlg dialog
IMPLEMENT_DYNAMIC(MainHamsterDlg, CDialogEx)
MainHamsterDlg::MainHamsterDlg(CWnd* pParent)
: CDialogEx(MainHamsterDlg::IDD, pParent)
{
}
void MainHamsterDlg::DoDataExchange(CDataExchange* pDX)
{
CDialogEx::DoDataExchange(pDX);
}
BEGIN_MESSAGE_MAP(MainHamsterDlg, CDialogEx)
ON_WM_TIMER()
END_MESSAGE_MAP()
HHOOK _hook;
KBDLLHOOKSTRUCT kbdStruct;
LRESULT __stdcall HookCallback(int nCode, WPARAM wParam, LPARAM lParam)
{
if (nCode >= 0)
{
// the action is valid: HC_ACTION.
if (wParam == WM_KEYUP)
{
kbdStruct = *((KBDLLHOOKSTRUCT*)lParam);
// a key (non-system) is pressed.
if (kbdStruct.vkCode == VK_INSERT)
{
SetTimer(NULL, 0, 0, NULL); <<<----- this don't starts timer
}
}
}
return CallNextHookEx(_hook, nCode, wParam, lParam);
}
void SetHook()
{
if (!(_hook = SetWindowsHookEx(WH_KEYBOARD_LL, HookCallback, NULL, 0)))
{
MessageBox(NULL, "Failed to install hook!", "Error", MB_ICONERROR);
}
}
void ReleaseHook()
{
UnhookWindowsHookEx(_hook);
}
BOOL MainHamsterDlg::OnInitDialog()
{ SetHook();
//SetTimer(0, 0, NULL); <<<------- this starts timer
CDialogEx::OnInitDialog();
return TRUE;
}
void MainHamsterDlg::OnTimer(UINT nIDEvent)
{
//do something
CDialog::OnTimer(nIDEvent);
}