C ++ accelerator keys do not work

I'm having problems working with Accelerators. I am using C ++.

After my window is configured and displayed.

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 works fine, and calls my field "O", there are no problems.

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 (works from the menu again)

 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 doesn't call the window. I went all over the Microsoft website, and was very careful, but I can’t find anything vivid that I do different.

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

Everything else works, but is that what I am missing ???

+4
source share
3 answers

Found it!

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

To distinguish the message that this function sends from messages sent using menus or controls, the high word of the wParam parameter of the WM_COMMAND or WM_SYSCOMMAND message contains a value of 1.

This was a problem, I checked wParam for a value of 0, I did not understand that it is 1 , if TranslateAcceleratorW sent

 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, it was a tiny detail.

+5
source

in your .rc resource, try removing VIRTKEY.

-1
source

You should try putting 0x41 instead of β€œa” in your .rc. This is the ascii code for β€œA” (uppercase ...)

-1
source

Source: https://habr.com/ru/post/1414686/


All Articles