Using a Hot Key in an ActiveX Control


For a normal executable, you use RegisterHotKey and WM_HOTKEY message handlers to add hotkey features, but the same code does not work in an ActiveX control. Others tell you that there is no message pump for an ActiveX control, so WM_HOTKEY doesn’t work. I have seen some implementations geting around the problem by WH_GETMESSAGE hook (Q187988 PRB: ActiveX Control Is the Parent Window of Modeless Dialog. Here I give you another way, by creating a hiden modeless dialog that enables a message pump to enable hotkeys without a Windows hook. Why a dialog? Other top-level windows also have a message pump, but a modeless dialog does not require an additional thread for the message loop.

#pragma once


// CHotkeyDlg dialog

class CHotkeyDlg : public CDialog
{
   DECLARE_DYNAMIC(CHotkeyDlg)

public:
   CHotkeyDlg(CWnd* pParent = NULL);   // standard constructor
   virtual ~CHotkeyDlg();

// Dialog Data
   enum { IDD = IDD_ABOUTBOX_MFCACTIVEXHOTKEY };

protected:
   virtual void DoDataExchange(CDataExchange* pDX);    // DDX/DDV
                                                       // support

   DECLARE_MESSAGE_MAP()
public:
   virtual BOOL OnInitDialog();
   afx_msg void OnWindowPosChanging(WINDOWPOS* lpwndpos);
   afx_msg void OnDestroy();
   LRESULT OnHotKey(WPARAM wParam, LPARAM lParam);
protected:
   virtual void PostNcDestroy();
};

// HotkeyDlg.cpp : implementation file
//

#include "stdafx.h"
#include "mfcactivexhotkey.h"
#include "HotkeyDlg.h"
#include ".hotkeydlg.h"


// CHotkeyDlg dialog

IMPLEMENT_DYNAMIC(CHotkeyDlg, CDialog)
CHotkeyDlg::CHotkeyDlg(CWnd* pParent /*=NULL*/)
   : CDialog(CHotkeyDlg::IDD, pParent)
{
}

CHotkeyDlg::~CHotkeyDlg()
{
}

void CHotkeyDlg::DoDataExchange(CDataExchange* pDX)
{
   CDialog::DoDataExchange(pDX);
}


BEGIN_MESSAGE_MAP(CHotkeyDlg, CDialog)
   ON_WM_WINDOWPOSCHANGING()
   ON_MESSAGE(WM_HOTKEY, OnHotKey)
   ON_WM_DESTROY()
END_MESSAGE_MAP()


// CHotkeyDlg message handlers

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

   // TODO:  Add extra initialization here
   ATOM aHotKey=GlobalAddAtom("F11");
   BOOL bret = RegisterHotKey(m_hWnd,aHotKey,MOD_CONTROL,VK_F11);
   if(bret)
      OutputDebugString("RegisterHotKey return OK");
   else
      OutputDebugString("RegisterHotKey return false");
   return TRUE;    // return TRUE unless you set the focus to
                   // a control
   // EXCEPTION: OCX Property Pages should return FALSE
}

void CHotkeyDlg::OnWindowPosChanging(WINDOWPOS* lpwndpos)
{
   if (lpwndpos->flags & SWP_SHOWWINDOW) {
      lpwndpos->flags &= ~SWP_SHOWWINDOW;
      PostMessage(WM_WINDOWPOSCHANGING, 0, (LPARAM)lpwndpos);
      ShowWindow(SW_HIDE);
   }
   else
      CDialog::OnWindowPosChanging(lpwndpos);
}

void CHotkeyDlg::OnDestroy()
{
   ATOM aHotKey=GlobalAddAtom("F11");
   UnregisterHotKey(m_hWnd,aHotKey);
   CDialog::OnDestroy();
}

LRESULT CHotkeyDlg::OnHotKey(WPARAM wParam, LPARAM lParam)
{
   AfxMessageBox("hotkey");
   return LRESULT(0);
}

void CHotkeyDlg::PostNcDestroy()
{
   CDialog::PostNcDestroy();
   delete this;
   OutputDebugString(__FUNCTION__);
}

Add a member to your control as. Create the modeless dialog in the constructor of the ActiveX:

CmfcactivexhotkeyCtrl::CmfcactivexhotkeyCtrl()
{
   InitializeIIDs(&IID_Dmfcactivexhotkey,
                  &IID_DmfcactivexhotkeyEvents);
   // TODO: Initialize your control's instance data here.
   AFX_MANAGE_STATE(AfxGetStaticModuleState());

   m_pHotKeyWnd = NULL;
   m_pHotKeyWnd = new CHotkeyDlg(0);
   m_pHotKeyWnd->Create(IDD_ABOUTBOX_MFCACTIVEXHOTKEY,
                           GetDesktopWindow());
}

Destroy the dialog in the destructor of the ActiveX:

// CmfcactivexhotkeyCtrl::~CmfcactivexhotkeyCtrl -
// Destructor CmfcactivexhotkeyCtrl::~CmfcactivexhotkeyCtrl() {
   if(m_pHotKeyWnd)
   {
      m_pHotKeyWnd->SendMessage(WM_CLOSE);
      m_pHotKeyWnd->SendMessage(WM_DESTROY);
      m_pHotKeyWnd->SendMessage(WM_NCDESTROY);
      OutputDebugString(__FUNCTION__);
   }
}

I saw someone posted a thread in www.csdn.net asking the question about how to use a hot key in an ActiveX control. I answered that question and gave the solution above. It may be useful for others too, so I’m posting it here. I have an MFC project if you prefer source code to textbook. Note, it is created with VC2003, on Windows 2003.

More by Author

Previous article
Next article

Get the Free Newsletter!

Subscribe to Developer Insider for top news, trends & analysis

Must Read