dialog box + accelerators

Is it possible to use accelerators in dialog box? In window-based program I have to use TranslateAccelerator function but how about dialog-based program?
It works the same way with dialogs as with windows. After all, a dialog box is nothing but a window.
The problem is that TranslateAccelerator and DispatchMessage functions must be in message loop and I can't do this for dialog box.
If your dialog box is modeless, you can handle the keystrokes by loading and destroying your accelerators. I have multiple dialogs boxes in an application with a number of different accelerator tables for each one. I simply load and destroy my accelerators as needed.
It doesn't work for me:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
//
// main.cpp
//

#include <windows.h>
#include "header.h"

HACCEL hacc;
HINSTANCE hinst; //global hinstance of this program

BOOL CALLBACK DlgProc(HWND hwnd,UINT message,WPARAM wParam,LPARAM lParam)
{
switch(message)
  {
     case WM_INITDIALOG:
          hacc=LoadAccelerators(hinst,MAKEINTRESOURCE(IAC_ACCELERATOR));
          return true;

     case WM_COMMAND:
          switch(LOWORD(wParam))
            {
                case IDA_DOSOMETHING:
                   MessageBox(hwnd,"Accelerator works!","OK",64); // This message box must show up when I press 'x' on keyboard
                   break;

            }
          break;

   default: return false;
  }

}




int WinMain(HINSTANCE hThis,HINSTANCE hPrev,LPSTR cmd,int xxx)
{
hinst=hThis;

DialogBox(GetModuleHandle(NULL),MAKEINTRESOURCE(IDD_DLG),0,DlgProc);



}

1
2
3
4
5
6
7
8
9
//
// header.h
//

#define IDD_DLG          665
#define IAC_ACCELERATOR 1234 // accelerator itself
#define IDA_DOSOMETHING 4567 // action



1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
//
// resource.rc
//

#include <windows.h>
#include "header.h"

// The dialog
IDD_DLG DIALOG 100,100,100,100
STYLE DS_MODALFRAME | DS_SETFONT | WS_BORDER | WS_CAPTION | WS_DLGFRAME | WS_POPUP | WS_SYSMENU
BEGIN

END


IAC_ACCELERATOR ACCELERATORS
BEGIN
    "x",            IDA_DOSOMETHING, ASCII
END

Or maybe it doesn't work for some other reason?
You can't do that with MessageBox(). You can only do this with real dialog boxes.
But this IS a dialog box and the message box must show up when I press 'x' on keyboard. As I said, in window-based program I used TranslateAccelerator() function:
1
2
3
4
5
6
7
  // message loop
    while (GetMessage (&messages, NULL, 0, 0))
    {
        TranslateAccelerator(hwnd,hacc,&messages); // <---
        TranslateMessage(&messages);
        DispatchMessage(&messages);
    }

but dialog box doesn't have message loop and accelerator isn't translated.
Oh, okay. I see now. It should work. Perhaps try this...

1
2
3
4
IAC_ACCELERATOR ACCELERATORS
BEGIN
     "x"               IDA_DOSOMETHING, VIRTKEY, CONTROL, NOINVERT
END


IOW, instead of using the ASCII designation, except in the above example you would press Ctrl+x

See if that does the trick.

Also, are you first destroying your initial accelerator before loading the second one?
Last edited on
After looking more carefully at your code, I THINK you must use a MODELESS dialog box in order to trap the key.
Topic archived. No new replies allowed.