0

Sample menu:

IDR_MENU_PUBLISHERS_DATABASE MENU
BEGIN
    POPUP "Database"
    BEGIN
        MENUITEM "Print\tCTRL + P",             ID_DATABASE_PRINT
    END
END

Handler:

void CPublishersDatabaseDlg::OnDatabasePrint()
{
    CFieldServiceGroupReportDlg dlgReport(this);

    dlgReport.DoModal();
}

Clicking the menu item handler works fine. But using CTRL + P does nothing. I tried creating am accelator table and using that approach by loading the table. No joy.

In the end I had to use PreTranslateMessage like this:

BOOL CPublishersDatabaseDlg::PreTranslateMessage(MSG* pMsg)
{
    BOOL    bNoDispatch = FALSE, bDealtWith = FALSE;

    if (IsCTRLpressed() &&
        pMsg->message == WM_KEYDOWN && pMsg->wParam == _TINT{ _T('P') })
    {
        OnDatabasePrint();
        // Eat it.
        bNoDispatch = TRUE;
        bDealtWith = TRUE;
    }

    if (!bDealtWith)
        bNoDispatch = __super::PreTranslateMessage(pMsg);

    return bNoDispatch;
}

Why?

Andrew Truckle
  • 17,769
  • 16
  • 66
  • 164
  • Maybe I just do things differently but adding the accelerator into the menu name in the resource file isn't the normal way to create an accelerator. You should create an `IDD_xxx ACCELERATORS` resource and add `"P", ID_DATABASE_PRINT, VIRTKEY, CONTROL` to that. The system will (should?) then add the 'Ctrl+P' text to the menu item. – Adrian Mole Jul 25 '23 at 10:26
  • The `IDD_xxx` will be the same as the ID for the `MENU` resource. – Adrian Mole Jul 25 '23 at 10:28
  • @AdrianMole Yes, that is what I tried too (with accelerator table). But we always use a tab and the key press to show on right. That is how boilerplate code is. I am wondering if control / p is hardwired to ID_FILE_PRINT or something deeper down. – Andrew Truckle Jul 25 '23 at 10:37
  • Hmm. Yeah - a conflict with another command may be the problem. I don't think there's any "hardwire" to "Print" but maybe that's added by your mainframe's menu/accelerator resources? – Adrian Mole Jul 25 '23 at 10:43
  • How is the menu you've shown loaded/created? If you do it manually, I guess you'll also have to load the accelerators manually, alongside the process. – Adrian Mole Jul 25 '23 at 10:44
  • In all cases the menus are indicated in the resource IDE for the dialog. No manual code. If I use accelerator tables then I do load / destroy them when the dialog is created / destroyed. All other hotkeys work in the accelerator tables. None of my tables have CTRL + P yet. Bu t like I said, even when I did use it, nothing happened. – Andrew Truckle Jul 25 '23 at 10:48
  • @AdrianMole Sorted. Forgot to add the required code to use `::TranslateAccelerator`. – Andrew Truckle Jul 25 '23 at 11:25

0 Answers0