c++winformswinapikeyboardcallback

How to handle keyboard events in a winapi standard dialog?


i don't often work with winapi, i'm writing almost .NET code. But at this time I have to use the winapi to make a simple dialog. There i want to handle some keyevents. Therefore i watched for the corresponding callback message WM_KEYDOWN or WM_KEYUP at MSDN and added it to my callback function.

INT_PTR CALLBACK cbfunc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam) {
  switch(message) {
    // ...

    case WM_KEYUP:
        MMsgBox("up"); // I never get here
        return 0;

    case WM_KEYDOWN:
        MMsgBox("down"); // I never get here        
        return 0;

    // ...
  }
  return 0;
}

But neither WM_KEYUP nor WM_KEYDOWN ever get triggered. Then I stated looking for a solution for this problem. I thought may my dialog eats this messages. So I added:

    case WM_GETDLGCODE: 
        return DLGC_WANTALLKEYS;

With the result that it doesn't help. Other solutions I've found were the following:

So none of them worked for me. In the moment i have got no idea how to handle it. Something I've noticed, is that on key press a WM_COMMAND message seems to occur.

Regards Nem.


Solution

  • According to this link, certain messages are hard to trap with dialog boxes because Windows processes them internally and they never get to the DialogProc. Here are two of the options I can think of:

    1. Use GetAsyncKeyState on a WM_COMMAND event
    2. Create a custom dialog box, the DialogProc for which will handle WM_KEYDOWN etc. messages.