Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

OnKeyDown (WM_KEYDOWN) in dialog based app

Status
Not open for further replies.

ProgramminFool

Technical User
Oct 26, 2003
10
CA
I create a MFC Dialog based app in MSVC++6

I get rid of all the controls (label, two buttons)

I add a windows message handler for WM_KEYDOWN

I change it so it shows a message with

AfxMessageBox("Hello", NULL, NULL);

Then I compile and run the program

If I press an arrow key, nothing happens! Why? The message shows when I press space, ctrl, almost anything else...how do I 'pick up' an arrow key from a dialog box?

Thanks for reading this!

-------------------------------------------------
“Talent is cheap, dedication is expensive. It will cost you your life.”
 
In order your dialog to respond for the any key pressed you should implement a function like following:
BOOL YourClass::processKeys(CWnd* pSender, UINT nMessage, UINT nChar, UINT nRepCnt, UINT nFlags)
based on the CWnd::OnChar(), CWnd::OnKeyDown, OnKeyUp() e.g to process WM_CHAR, WM_KEYDOWN, WM_KEYUP etc...

BOOL YourClass::processKeys(CWnd* pSender, UINT nMessage, UINT nChar, UINT nRepCnt, UINT nFlags)
{
static bool m_bShiftDown = false;
if (nChar == VK_TAB && nMessage = WM_CHAR
{
// TAB key
}
if (nChar == VK_SHIFT )
{
if (nMessage = WM_KEYDOWN )
{
m_bShiftDown = true;

}
if (nMessage = WM_KEYUP )
{
m_bShiftDown = false;
}
}
if (nChar == VK_LEFT )
{
//Left Arrow
}
if (nChar == VK_DOWN)
{
// Down Arrow
}

}
-obislavu-
 
I think I understand that much; I know how to use a switch statement or if statement to determine the VALUE of nChar.

However, if one does as I say, the message will appear no matter what key is pressed - nChar is not checked...so shouldn't the WM_KEYDOWN message be passed to my dialog box no matter WHAT the virtual keycode is?

I create a function like this:

void CKeys4Dlg::OnKeyDown(UINT nChar, UINT nRepCnt, UINT nFlags)
{
AfxMessageBox("hello", NULL, NULL);
CDialog::OnKeyDown(nChar, nRepCnt, nFlags);
}

Using the classwizard in vc++6 to add a 'windows message handler'

And the message box does not appear if I hit the arrow buttons, yet it does for any other key on my keyboard (obviously not alt and such)

btw, now I have an even stranger problem: I tried going through the process on a different computer (it has a different service pack, would that change anything?) and now I can get the message when its a left arrow or up arrow, but not down or right.
Is that weird or what?!

-------------------------------------------------
“Talent is cheap, dedication is expensive. It will cost you your life.”
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top