0

I have to create a page using visual studio c++ with different menus having shortcuts (key accelerator).The shortcut should be like the way we have in windows notepad eg.(alt + f for files,alt + e for edit) etc.I want to know how to implement my own alt+key shortcut for the menu items that i have in my page.Please help.

1 Answers1

0

First, you'll need to define an ACCELERATORS resource in your resource file (*.rc). The MSDN docs give this example of an accelerator table.

1 ACCELERATORS
{
    "^C",  IDDCLEAR         ; control C
    "K",   IDDCLEAR         ; shift K
    "k",   IDDELLIPSE, ALT  ; alt k
    98,    IDDRECT, ASCII   ; b
    66,    IDDSTAR, ASCII   ; B (shift b)
    "g",   IDDRECT          ; g
    "G",   IDDSTAR          ; G (shift G)
    VK_F1, IDDCLEAR, VIRTKEY                ; F1
    VK_F1, IDDSTAR, CONTROL, VIRTKEY        ; control F1
    VK_F1, IDDELLIPSE, SHIFT, VIRTKEY       ; shift F1
    VK_F1, IDDRECT, ALT, VIRTKEY            ; alt F1
    VK_F2, IDDCLEAR, ALT, SHIFT, VIRTKEY    ; alt shift F2
    VK_F2, IDDSTAR, CONTROL, SHIFT, VIRTKEY ; ctrl shift F2
    VK_F2, IDDRECT, ALT, CONTROL, VIRTKEY   ; alt control F2
}

You'll compile the resource file (with RC, the Microsoft resource compiler) and link the resulting *.res file with your application.

Next, in your WinMain, call LoadAccelerators (see the MSDN for the syntax) with the identifier of your resource table so the application has access to it. For the table above, you could do

HACCEL hAccel = LoadAccelerators(hInstance, 1);

where hInstance is the HINSTANCE of your application, and the 1 is the identifier of the table.

Finally, call TranslateAccelerator (again, see the MSDN for the syntax) in your message loop, after GetMessage, to be able to handle accelerator messages. Again, in this example, you could do

TranslateAccelerator(hwnd, hAccel, &msg);

where hwnd is the HWND of your main window and msg is the MSG structure defined for your main window.

If you do all this correctly, your application should receive a message via WM_COMMAND whenever an accelerator event occurs, and the wParam field of the message will contain the identifier of the key event that was triggered (the IDD* constants in the example table).

emprice
  • 912
  • 11
  • 21