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.

Edited by Learning78: Wrong code

8 Years
Discussion Span
Last Post by Learning78

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.

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.