-
Simple keyboard capture
Anyone have any idea why the following code doesn't work? Whenever I press any of the keys, nothing happens. If it were to work right, it would change the mouse cursor..
Code:
void CMouseDlg::OnKeyDown(UINT nChar, UINT nRepCnt, UINT nFlags)
{
char lsChar;
HCURSOR lhCursor;
// TODO: Add your message handler code here and/or call default
lsChar = char(nChar);
if (lsChar == 'A')
{
lhCursor = AfxGetApp()->LoadStandardCursor(IDC_ARROW);
SetCursor(lhCursor);
}
if (lsChar == 'B')
{
lhCursor = AfxGetApp()->LoadStandardCursor(IDC_IBEAM);
SetCursor(lhCursor);
}
if (lsChar == 'C')
{
lhCursor = AfxGetApp()->LoadStandardCursor(IDC_WAIT);
SetCursor(lhCursor);
}
if (lsChar == 'X')
{
lhCursor = AfxGetApp()->LoadStandardCursor(IDC_ARROW);
SetCursor(lhCursor);
OnOK();
} else {
m_bCursor = TRUE;
SetCursor(lhCursor);
}
CDialog::OnKeyDown(nChar, nRepCnt, nFlags);
}
-
This is a guess so please forgive me if it doesn't work (I don't use mfc).
Code:
void CMouseDlg::OnKeyDown(UINT nChar, UINT nRepCnt, UINT nFlags)
{
/*nChar is a UINT to cover UNICODE and ANSI so there's little point in casting it*/
/* similarly your HCURSOR is a local variable (does mfc, automatically free cursor resources when they go out of scope?)*/
switch (nChar)
{
case TEXT('A'):
::SetCursor(AfxGetApp()->LoadStandardCursor(IDC_ARROW);
break;
case TEXT('B'):
::SetCursor(AfxGetApp()->LoadStandardCursor(IDC_IBEAM));
break;
case TEXT('C'):
::SetCursor(AfxGetApp()->LoadStandardCursor(IDC_WAIT));
break;
case TEXT('X'):
::SetCursor(AfxGetApp()->LoadStandardCursor(IDC_WAIT));
OnOK();
break;
default:
m_bCursor = TRUE;
::SetCursor(0); //? not sure about this
}
CDialog::OnKeyDown(nChar, nRepCnt, nFlags);
}
If that works ok then the original problem was that your HCURSOR was of local scope and therefore died off at the end of the fn. Try defining it with class scope.
Sorry for re-writing your code - it was to make it easier for simple me to follow. :)
I hope that's of some use to you.