title: How to handle keyboard events in a  winapi standard dialog?
body: <p>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 <a href="http://msdn.microsoft.com/en-us/library/ms646280%28VS.85%29.aspx" rel="nofollow"><code>WM_KEYDOWN</code> or <code>WM_KEYUP</code></a> at MSDN and added it to my callback function.</p>
  
  <pre><code>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;
  }
  </code></pre>
  
  <p>But neither <code>WM_KEYUP</code> nor <code>WM_KEYDOWN</code> ever get triggered. Then I stated looking for a solution for this problem. I thought may my dialog eats this messages. So I added:</p>
  
  <pre><code>    case WM_GETDLGCODE: 
          return DLGC_WANTALLKEYS;
  </code></pre>
  
  <p>With the result that it doesn't help.
  Other solutions I've found were the following:</p>
  
  <ul>
  <li>Alternatively using the <code>WM_GETDLGCODE</code> event to handle this keys as suggested on <a href="http://msdn.microsoft.com/en-us/library/community/history/ms646280%28v=VS.85%29.aspx?id=4" rel="nofollow">here</a>.</li>
  <li>I've found a lot of threads (like <a href="http://www.eggheadcafe.com/software/aspnet/35982805/pretranslatemessage.aspx" rel="nofollow">this one</a>) talking about a method called <code>PreTranslateMessage</code>. But I don't even have got this class, because I simply create my dialog by using <a href="http://msdn.microsoft.com/en-us/library/ms645465%28v=vs.85%29.aspx" rel="nofollow"><code>DialogBoxParam</code></a></li>
  </ul>
  
  <p>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 <code>WM_COMMAND</code> message seems to occur.</p>
  
  <p>Regards Nem.</p>