0

A few months ago here on StackOverflow someone encouraged me to do things properly and use hotkeys. As I result I followed their advice.

I have several hotkeys allocated in my Editor and I have just encountered an issue. Here is one of the hotkeys:

if (!RegisterHotKey(GetSafeHwnd(), hkEditor_WeekendMeeting, MOD_CONTROL | MOD_SHIFT, 0x57)) // W
    aryStrHotKeyErrors.Add(_T("Control + Shift + W"));

The hotkey event handler:

void CChristianLifeMinistryEditorDlg::OnHotKey(UINT nHotKeyId, UINT nKey1, UINT nKey2)
{
    switch (nHotKeyId)
    {
    case hkEditor_WeekendMeeting:
        OnFilePublicTalk();
        break;
    default:
        CDialogEx::OnHotKey(nHotKeyId, nKey1, nKey2);
    }
}

I have removed out all the other hotkeys for clarity. OnFilePublicTalk displays a popup modal dialog:

void CChristianLifeMinistryEditorDlg::OnFilePublicTalk()
{
    CPublicTalkDlg dlgPublicTalk(this);

    if (m_pEntry != nullptr)
    {
        dlgPublicTalk.SetPublicTalkInfo(m_pEntry->GetPublicTalkInfo());
        dlgPublicTalk.SetCircuitVisitMode(m_iIncludeMode == kIncludeServiceTalk); // AJT v17.0.7
        if (dlgPublicTalk.DoModal() == IDOK)
        {
            m_pEntry->SetPublicTalkInfo(dlgPublicTalk.GetPublicTalkInfo());

            SetModified(true);
            UpdatePreview(m_iDateIndex);
            m_pHtmlPreview->Refresh2(REFRESH_COMPLETELY); // Ensure it has refreshed
        }
    }
}

The hotkey works fine. However, if I am inside another modal popup window in my editor at the time and accidently press the hotkey, up comes the window. I did not expect any of my hotkeys to function if popup windows are displayed.

How do I correct this?

Andrew Truckle
  • 17,769
  • 16
  • 66
  • 164
  • 1
    Why are you using hot keys instead of accelerators? If you look at the documentation for hotkeys, you will see that they are system wide. It should not be surprising that pressing them in another modal dialog in your program causes the action to happen. It will happen if someone is using Word and presses the same hotkey combination. – Joseph Willcoxson Jan 14 '18 at 20:31
  • @JoeWillcoxson How do you set up accelerators then? – Andrew Truckle Jan 14 '18 at 21:54
  • @JoeWillcoxson I saw this: https://stackoverflow.com/questions/10702437/adding-acceleratorsshortcuts-in-mfc-how. This is where I was told about hotkeys: https://stackoverflow.com/questions/46817844/detect-keyboard-hotkey-inside-edit-control-of-ccombobox. My menu already has accelerators is the resources by using "\t" followed by the key press but they were not always being fired if certain controls had focus. – Andrew Truckle Jan 14 '18 at 22:14
  • @JoeWillcoxson Ah, will try this: http://www.blackbeltcoder.com/Articles/mfc/implement-accelerators-in-an-mfc-dialog-box – Andrew Truckle Jan 14 '18 at 22:19
  • I don't have my code in front of me and don't want to login to VPN to look at my code. Usually, you create an accelerator table. Then, in PreTranslateMessage() you have a crack to handle a WM_KEYDOWN message before the control gets a chance at it. In the PreTranslateMessage() for your dialog, look at the key combination and see if it matches a key in the HACCEL (accelerator table). I'd look at accelerator table functions and see if there is one that does all the work for you and returns a return code saying whether it handled it or not. Look at code for CMDIFrameWnd::OnTranslateMessage(). – Joseph Willcoxson Jan 14 '18 at 22:23
  • Yeah, look at that article. – Joseph Willcoxson Jan 14 '18 at 22:24

1 Answers1

0

I came up with this solution:

void CChristianLifeMinistryEditorDlg::OnHotKey(UINT nHotKeyId, UINT nKey1, UINT nKey2)
{
    if (GetActiveWindow() != this)
    {
        CDialogEx::OnHotKey(nHotKeyId, nKey1, nKey2);
        return;
    }

    switch (nHotKeyId)
    {
    case hkEditor_WeekendMeeting:
        OnFilePublicTalk();
        break;
    default:
        CDialogEx::OnHotKey(nHotKeyId, nKey1, nKey2);
    }
}

I used GetActiveWindow. Was it the right way to resolve this?

Andrew Truckle
  • 17,769
  • 16
  • 66
  • 164