Ig_M
Ig_M

Reputation: 160

Handling the hotkey for menu item

I have created a new menu item "Extra" and add it to the window's main menu.

HMENU menu = GetMenu(hWnd);
HMENU popup = CreatePopupMenu();
AppendMenu(menu, MF_STRING | MF_POPUP, (UINT)popup, L"Extra");
SetMenu(hWnd, popup);

Next I inserted new item to "Extra" item with hotkey:

#define IDM_ITEM1 12310

and:

MENUITEMINFOW mmi;
mmi.cbSize = sizeof(MENUITEMINFOW);
mmi.fMask = MIIM_STRING | MIIM_ID;
mmi.fType = MFT_STRING;
mmi.dwTypeData = L"First item\tCtrl+N";
mmi.wID = IDM_ITEM1;
InsertMenuItemW(popup, 1, true, &mmi);

enter image description here

And handle "First item" click:

case WM_COMMAND:
{
    switch(LOWORD(wParam))
    {
        case IDM_ITEM1:
        {
        MessageBox(0, L"First", L"", MB_OK);
        break;
        }
    }

break;
}

And now when I click on "First item" a message box is appeared.

But when I press Ctrl+N hotkey - nothing happens. Why? How to fix this (without using accelerator resources)?

Upvotes: 0

Views: 290

Answers (1)

CGi03
CGi03

Reputation: 650

without using accelerator resources ?

You need to create an array of ACCEL and then call the CreateAcceleratorTable function:

ACCEL s_accel[2] = {{FCONTROL | FVIRTKEY, TEXT('C'), IDM_COLOR}, 
                    {FCONTROL | FVIRTKEY, TEXT('Q'), IDM_QUIT}};
HACCEL h_accel = CreateAcceleratorTable(s_accel, 2);

MSG msg;
while (GetMessage(&msg, NULL, 0, 0))
{
  if (!TranslateAccelerator(hwnd, h_accel, &msg))
      {
        TranslateMessage(&msg);
        DispatchMessage(&msg);
      }
}
DestroyAcceleratorTable(h_accel);

Upvotes: 1

Related Questions