开发者

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 th开发者_如何学运维e 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:

  • Alternatively using the WM_GETDLGCODE event to handle this keys as suggested on here.
  • I've found a lot of threads (like this one) talking about a method called PreTranslateMessage. But I don't even have got this class, because I simply create my dialog by using DialogBoxParam

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.


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.


DialogProc doesn't receive WM_KEY events (and many others too). You can:

  1. Subclass the dialog window (overwrite its WndProc) and process all messages there, sample
  2. Register hot key for the dialog window's HWND and then receive WM_HOTKEY in DlgProc (but registered key combinations will be system-wide)
  3. Create your own message loop, link


Replace This

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

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

With This

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

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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜