Hi;
I have made a MCQ based game in Visual C++ using MFC. It runs fine but it shuts down when some key is pressed unintentionally. Like, if you click the options with mouse click, it works fine but even if you press ENTER key during the game, it quits immediately. What could be the reason? Does it have anything to do with Keyboardstate declaration? If anybody had such experience, kindly share your solution.

That's a famous behavior of MFCs Close button -- the Close button is activated when the Enter key is pressed. One way to fix that is to catch the WM_CLOSE event and ignore it, but then you might not be able to close the program at all.

Another way is to catch the return key in OnTranslateMessage(). This is for CDialog, but you can do the same thing in any type of MFC program.

BOOL CTestmfcDlg::PreTranslateMessage(MSG* pMsg) 
{
	if( pMsg->message == WM_KEYDOWN && pMsg->wParam == VK_RETURN )
	{
		return FALSE;
	}
	return CDialog::PreTranslateMessage(pMsg);
}

Thanks a lot Ancient Dragon. Your reply really helped to solve the problem.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.