News:

GinGly.com - Used by 85,000 Members - SMS Backed up 7,35,000 - Contacts Stored  28,850 !!

Main Menu

Effects: Key Down and Key Up

Started by sukishan, Jul 12, 2009, 05:42 PM

Previous topic - Next topic

sukishan

Keyboard Messages

The Key Down Effect

When we think of the keyboard, the first thing that comes in mind is to press a key. When a keyboard key is pressed, a message called WM_KEYDOWN is sent. Its syntax is:

afx_msg void OnKeyDown( UINT nChar, UINT nRepCnt, UINT nFlags );The first argument, nChar, specifies the virtual code of the key that was pressed.

The second argument, nRepCnt, specifies the number of times counted repeatedly as the key was held down.

The nFlags argument specifies the scan code, extended-key flag, context code, previous key-state flag, and transition-state flag.

Practical Learning: Sending Key Down Messages
To experiment with the ON_KEYDOWN message, change the file as follows:  Collapse Copy Code#include <AFXWIN.H>

class CMainFrame : public CFrameWnd
{
public:
    CMainFrame ();

protected:
    afx_msg int  OnCreate(LPCREATESTRUCT lpCreateStruct);
    afx_msg void OnKeyDown(UINT nChar, UINT nRepCnt, UINT nFlags);

    DECLARE_MESSAGE_MAP()
};

CMainFrame::CMainFrame()
{
    // Create the window's frame
    Create(NULL, "Windows Application", WS_OVERLAPPEDWINDOW,
           CRect(120, 100, 700, 480), NULL);
}

class CExerciseApp: public CWinApp
{
public:
    BOOL InitInstance();
};

BEGIN_MESSAGE_MAP(CMainFrame, CFrameWnd)
    ON_WM_CREATE()
    ON_WM_KEYDOWN()
END_MESSAGE_MAP()

int CMainFrame::OnCreate(LPCREATESTRUCT lpCreateStruct)
{
    if (CFrameWnd::OnCreate(lpCreateStruct) == -1)
        return -1;
    return 0;
}

void CMainFrame::OnKeyDown(UINT nChar, UINT nRepCnt, UINT nFlags)
{
    switch(nChar)
    {
    case VK_RETURN:
        SetWindowText("You pressed Enter");
        break;
    case VK_F1:
        SetWindowText("Help is not available at the moment");
        break;
    case VK_DELETE:
        SetWindowText("Can't Delete This");
        break;
    default:
        SetWindowText("Whatever");
    }
}

BOOL CExerciseApp::InitInstance()
{
    m_pMainWnd = new CMainFrame ;
    m_pMainWnd->ShowWindow(SW_SHOW);
    m_pMainWnd->UpdateWindow();

    return TRUE;
}

CExerciseApp theApp;Test the application and return to MSVC.


The Key Up Effect

When we think of the keyboard, the first thing that comes in mind might refer to typing, which consists of pressing a key and releasing it immediately. As this is done, a key is pressed down and brought back up. When the user is releasing a key a WM_KEYUP message is sent. Its syntax is:

afx_msg void OnKeyUp(UINT nChar, UINT nRepCnt, UINT nFlags);
The first argument, nChar, is the code of the key that was pressed. The second argument, nRepCnt, specifies the number of times counted repeatedly as the key was held down.

The nFlags argument specifies the scan code, extended-key flag, context code, previous key-state flag, and transition-state flag.
A good beginning makes a good ending