Effects: Key Down and Key Up

Started by sukishan, Jul 12, 2009, 11:12 PM

previous topic - next topic
Go Down

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

Go Up
 

Quick Reply

With Quick-Reply you can write a post when viewing a topic without loading a new page. You can still use bulletin board code and smileys as you would in a normal post.

Warning: this topic has not been posted in for at least 120 days.
Unless you're sure you want to reply, please consider starting a new topic.

Note: this post will not display until it's been approved by a moderator.
Name:
Email:
Verification:
Please leave this box empty:

Type the letters shown in the picture
Listen to the letters / Request another image

Type the letters shown in the picture:

shortcuts: alt+s submit/post or alt+p preview
IT Acumens Web Designing Chennai | GinGly :: Build your Personal Website | CineBuzz :: Cinema News | My Kids Diary :: Kids Memories Writing :: Book Website @ 349 Rs monthly
Copyright 2005 - 2020 :: IT Acumens :: All Rights Reserved. :: Sitemap
ITAcumens Discussion Forum with 2 lakhs post running for 15 years - Powered by IT Acumens Dedicated Server