Thread: Simple keyboard capture

  1. #1
    Registered User Dual-Catfish's Avatar
    Join Date
    Sep 2001
    Posts
    802

    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);
    }

  2. #2
    erstwhile
    Join Date
    Jan 2002
    Posts
    2,227
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Keyboard hook
    By joecaveman in forum Windows Programming
    Replies: 2
    Last Post: 09-03-2005, 08:07 AM
  2. What type of keyboard is this?
    By 7smurfs in forum Tech Board
    Replies: 5
    Last Post: 06-22-2005, 05:09 PM
  3. Request for comments
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 01-02-2004, 10:33 AM
  4. Game Design Topic #2 - Keyboard or Mouse?
    By TechWins in forum Game Programming
    Replies: 4
    Last Post: 10-08-2002, 03:34 PM
  5. : Difference between message queue and keyboard buffer...
    By Unregistered in forum Windows Programming
    Replies: 0
    Last Post: 02-21-2002, 03:47 PM