C++ Accelerator keys do not work

3.3k views Asked by At

I am having trouble getting Accelerators to work. I am using C++.

After my window is setup and shown.

MENUITEMINFOW mAbout;
mAbout.cbSize = sizeof(MENUITEMINFO);
mAbout.fMask  = MIIM_TYPE | MIIM_ID;
mAbout.wID    = (UINT) ID_ABOUT;
mAbout.fType  = MFT_STRING;
mAbout.dwTypeData = (LPWSTR)L"&About";

InsertMenuItemW(HelpMenu,   0, TRUE, &mAbout);

My menu is working just fine, and calls my "About" box, no issue there.

Now, before the message loop, I load the accelerators:

// Load accelerators.
 HACCEL hAccelerators = LoadAcceleratorsW(hInstance, MAKEINTRESOURCEW(IDR_ACCELERATOR));

Then my main message loop:

while(GetMessageW(&msg, NULL, 0, 0) > 0) {
    if (! TranslateAcceleratorW(msg.hwnd, hAccelerators, &msg)) {
        TranslateMessage(&msg);
        DispatchMessageW(&msg);
    }
}

My WndProc Message handle (Again, works from the menu)

case WM_COMMAND: {
    if (HIWORD(wParam) == 0) {
        if (LOWORD(wParam) == 101) {
            testDialog(hInstance ,hWnd,(LPSTR)"Testing");
        }
        if (LOWORD(wParam) == ID_ABOUT) {
            DialogBox(hInstance, MAKEINTRESOURCE(IDD_ABOUTDIALOG), hWnd, &AboutDialogProc);
            return 0;
        }
    }
    break;
}

My resource.rc file:

//
// Accelerator resources
//
LANGUAGE LANG_NEUTRAL, SUBLANG_NEUTRAL
IDR_ACCELERATOR ACCELERATORS
{
    "a",    ID_ABOUT, VIRTKEY, ALT
}

and my resource.h file:

#define IDR_ACCELERATOR                         122
#define ID_ABOUT                                401

And... well, Alt-a does not bring up the about box. I have walked all over the Microsoft Website, and was vary careful, but I can't find anything glaring I am doing different.

I am on Windows 7 (64bit) using MinGW and compiling in the application for Unicode.

Everything else works but this, what am I missing???

3

There are 3 answers

0
Quade2002 On BEST ANSWER

Found it!

From: http://msdn.microsoft.com/en-us/library/windows/desktop/ms646373(v=vs.85).aspx

To differentiate the message that this function sends from messages sent by menus or controls, the high-order word of the wParam parameter of the WM_COMMAND or WM_SYSCOMMAND message contains the value 1.

This was the issue, I was checking wParam for a value of 0, I didn't catch that it is 1 if sent by TranslateAcceleratorW

if (HIWORD(wParam) == 0) {
    if (LOWORD(wParam) == 101) {
        testDialog(hInstance ,hWnd,(LPSTR)"Testing");
    }
    if (LOWORD(wParam) == ID_ABOUT) {
        DialogBox(hInstance, MAKEINTRESOURCE(IDD_ABOUTDIALOG), hWnd, &AboutDialogProc);
        return 0;
    }
} else if (HIWORD(wParam) == 1) {    // Accelerator input 
    if (LOWORD(wParam) == ID_ABOUT) {
        DialogBox(hInstance, MAKEINTRESOURCE(IDD_ABOUTDIALOG), hWnd, &AboutDialogProc);
        return 0;
    }
}

Geeze, that was a tiny detail.

0
sithereal On

in your resource.rc, try to remove VIRTKEY.

1
pagra On

You should try to put 0x41 instead of "a" in your .rc It's the ascii code for 'A' (uppercase...)