3

I was trying to use the following code to listen to simple keyboard events in a dialog based application, but nothing is happening on pressing these keys. Can somebody tell me what went wrong ?

BOOL PreTranslateMessage(MSG *pMsg)
{

CListeningApp* App = (CListeningApp*) AfxGetApp();
int x=(int)pMsg->wParam;

if(pMsg->message==WM_KEYDOWN)
{
    if(x==VK_UP)
    MessageBox(NULL,L"UP",L"UP", MB_OK);

    if(x==VK_DOWN)
    MessageBox(NULL,L"DOWN",L"DOWN", MB_OK);

    if(x==VK_RIGHT)
    MessageBox(NULL,L"RIGHT",L"RIGHT", MB_OK);

    if(x==VK_LEFT)
    MessageBox(NULL,L"LEFT",L"LEFT", MB_OK);

    if(x==65)
        MessageBox(NULL,L"Keyboard Event",L"You Pressed the letter a", MB_OK);


}
return TRUE;

}
Tarang Gupta
  • 139
  • 1
  • 3
  • 13
  • Because nobody calls `PreTranslateMessage`. Your `PreTranslateMessage` is just a standalone function. You could rename your `PreTranslateMessage` to `foobar`, the result would be the same. Read [this](https://msdn.microsoft.com/library/kkbhxcs2.aspx). – Jabberwocky Feb 12 '16 at 13:14
  • 2
    Just a suggestion: never ever debug anything with the `MessageBox()`, especially not the keyboard event handler. Use `::OutputDebugString()` and watch for it in the debugger's Output window. – Vlad Feinstein Feb 12 '16 at 16:34
  • @VladFeinstein it works here for testing purposes, but you are right `OutputDebugString` is better. – Jabberwocky Feb 12 '16 at 16:58
  • Handling key messages is a lot of work if you have many controls in dialog box --- If this is related to your earlier question, make sure the Scroll bar control has `WS_TABSTOP` flag (look for "Tab" flag in dialog editor), overload dialog's `WM_LBUTTONDOWN` to set focus to the desired control --- or use Slider control instead – Barmak Shemirani Feb 12 '16 at 19:08
  • @BarmakShemirani this one is not related to my previous query, its a blank dialog-based app with no components, so can you give me a suggestion in that perspective ? – Tarang Gupta Feb 15 '16 at 04:06
  • 1
    You are on the right track then. See Michael Walz's answer, make sure you call the base class: `return CDialog::PreTranslateMessage(pMsg);` otherwise it crashes. Only if you process the message, then `TRUE` to indicate that you have handled the message yourself. – Barmak Shemirani Feb 15 '16 at 04:17
  • @BarmakShemirani it is crashing in both the return cases.. – Tarang Gupta Feb 15 '16 at 04:20
  • @BarmakShemirani it is giving me a LNK2001 unresolved extrnal symbol error – Tarang Gupta Feb 15 '16 at 04:20
  • 1
    The link error is from something else. Read the full link error properly, it should say what it is. Rebuild the project if necessary. – Barmak Shemirani Feb 15 '16 at 04:25

1 Answers1

4

Your PreTranslateMessage function is a stand alone function. It won't be called automatically just because it's name is PreTranslateMessage.

The function must be member of your dialog class.

BOOL CYourDlg::PreTranslateMessage(MSG* pMsg)
{
...
}

CYourDlg being the your dialog class.

And somewhere in the declaration of your dialog class CYourDlg put:

virtual BOOL PreTranslateMessage(MSG* pMsg);
Jabberwocky
  • 48,281
  • 17
  • 65
  • 115