Win32 api edit control and keyboard accelerator

I am writing a simple notepad editor (configured for some additional functions) in win32 api. Editing management fills the application area and constantly focuses. I also need to handle some keyboard commands, such as Ctl-S. So I usually use the keyboard accelerator table to determine the Ctl-S key, and in my message loop I have TranslateAccelerator

while (GetMessage(&Msg,NULL,0,0)>0) { if (!TranslateAccelerator(Msg.hwnd,HAccel,&Msg)) { TranslateMessage(&Msg); DispatchMessage(&Msg); } } 

Now my problem is that since the edit window always has focus when the user types Ctl-S, I don’t get the WM_COMMAND message at all. (I understand that HIWORD of wParam will become 1 for a keyboard accelerator, but that is not a problem).

  case WM_COMMAND: switch (LOWORD(wParam)) { ... case ID_CTL_S_PRESSED: {My code here} break; ... } 

If I try the code without the Edit control, I will get the WM_COMMAND message above. So, how do I get the WM_COMMAND message for the keyboard accelerator when the edit control always has focus?

+6
source share
1 answer

The first TranslateAccelerator parameter is documented as:

handle to the window whose messages are to be translated.

This is misleading and not entirely correct. The section on WM_COMMAND message processing (using keyboard accelerators) is more appropriate:

When an accelerator is used, the window specified in the TranslateAccelerator function receives a WM_COMMAND or WM_SYSCOMMAND message.

To fix the problem, replace the call with TranslateAccelerator as follows:

 if (!TranslateAccelerator(hwndMain,HAccel,&Msg)) 

Replacing Msg.hwnd window handle to the main window will send the WM_COMMAND message WM_COMMAND you want.

+11
source

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


All Articles