2
votes

I have a window with a menu and an edit class handle. In the menu I have an Edit section with some options like cut, copy, paste, ecc.

I have defined 2 keyboard accelerators:

IDR_ACCELERATOR2 ACCELERATORS
BEGIN
    "A",            ID_EDIT_SALL,           VIRTKEY, CONTROL, NOINVERT
    "Z",            ID_EDIT_UNDO,           VIRTKEY, CONTROL, NOINVERT
END

CTRL + Z works, but CTRL + A doesn't. In the WM_COMMAND case I have this:

switch (LOWORD(wParam))
            {
            case ID_EDIT_CLEAR:
                SendMessage(hwndEdit, WM_CLEAR, 0, 0);
                break;
            case ID_EDIT_COPY:
                SendMessage(hwndEdit, WM_COPY, 0, 0);
                break;
            case ID_EDIT_CUT:
                SendMessage(hwndEdit, WM_CUT, 0, 0);
                break;
            case ID_EDIT_PASTE:
                SendMessage(hwndEdit, WM_PASTE, 0, 0);
                break;
            case ID_EDIT_SALL:
                SendMessage(hwndEdit, EM_SETSEL, 0, -1);
                break;
            case ID_EDIT_UNDO:
                SendMessage(hwndEdit, WM_UNDO, 0, 0);
                break;
            }

When I click on the menu option Select All, the option works well, it does select all the text in the editor handle, but when I try to use the keyboard combination CTRL+A Windows makes that error sound (like when you try to delete words in an empty document)

Update: the message loop

BOOL msgCheck;    

while ((msgCheck = GetMessage(&msg, NULL, 0, 0)) != 0)
        {
            if (msgCheck == -1)
            {
                MessageBox(NULL, TEXT("Error!"), TEXT("Error"), MB_ICONERROR);
                return -1;
            }
            else
            {
                TranslateMessage(&msg);
                DispatchMessage(&msg);
            }
        }
1
Just for testing, invert A and Z in your IDR_ACCELERATOR2 accelerator table and see what happens.Jabberwocky
Ok, I think CTRL+Z is controlled by the system, cause it didnt' change anything. Now I tried to add CTRL+L (not a "system" combination) linked to the CUT option and it doesn't work either.Astinog
You are right, Ctrl-Z is handled by Windows.Jabberwocky
Ok, but I don't understand why Ctrl+Z works and Ctrl+A doesn't. It's a bit strange...Astinog
Ctrl+Z works because it is entirely handled by Windows. Try tu put a breakpoint on the case ID_EDIT_UNDO: case, I bet it is not hit when you press Ctrl+Z.Jabberwocky

1 Answers

1
votes

In order to use accelerators, you need to load accelerator table first. Then you need to check for them in the message loop.

You haven't showed us if you have loaded the accelerator table, so I have showed you how to do it below:

HANDLE hAccel = LoadAccelerators( hInstance, MAKEINTRESOURCE(IDR_ACCELERATOR2));

You should do this inside WinMain, and then modify your message loop like below:

BOOL msgCheck;    

while ((msgCheck = GetMessage(&msg, NULL, 0, 0)) != 0)
        {
            if (msgCheck == -1)
            {
                MessageBox(NULL, TEXT("Error!"), TEXT("Error"), MB_ICONERROR);
                return -1;
            }
            else
            {
                // Add the below call to your message loop
                if(!TranslateAccelerator(hwnd, hAccel, &msg))
                {
                    TranslateMessage(&msg);
                    DispatchMessage(&msg);
                }
            }
        }

hwnd is the handle of the main window, hAccel is the handle to the accelerator table you loaded with the previous code snippet.

To better understand what I did and why, check out official documentation for accelerator tables, especially the code examples I linked to.

Just by reading through it, you will see what your mistakes are and will be able to understand what I did to fix them.